Is a new instance created in parentheses immediately after the class name?

// in a.h
#include <iostream>
#include <vector>

typedef std::vector<double> Array;
class A
{
    public:
          A(int n);
    private:
          Array m;
};

//in a.cpp
#include "a.h"
A::A(int n)
{
    m = Array(n, 0.0);
}

I want to initialize m in constructor A. Is the expression of parentheses some parameters immediately after the class name ( std::vector<double>) legal?

And what is the difference between     Array m(n,0.0)   as well     m=Array(n,0.0)?

+4
source share
1 answer
  • Yes, it is legal. ClassName()calls the constructor of this class.

    Note. Technically, the constructor does not have a name, so it cannot be found while searching for the name, therefore ClassName()it is really an explicit type conversion using functional notation, which _results in_ calls the constructor (as in C ++ 12.1.2 standard).

  • Array m(n,0.0) m Array, Array, 3 .

    MyClass m = Array(n,0.0) Array, Array , m, , , , elision. , MyClass m; m = Array(n,0.0), , .

+5

Source: https://habr.com/ru/post/1608677/


All Articles