Which constructor is being called here?

In this piece of code, which constructor is actually called?

Vector v = getVector(); 

The vector has a copy constructor, a default constructor, and an assignment operator:

 class Vector { public: ... Vector(); Vector(const Vector& other); Vector& operator=(const Vector& other); }; 

getVector returns a value.

 Vector getVector(); 

The code uses the C ++ 03 standard.

The code snippet looks like it should call the default constructor and then the assignment operator, but I suspect this declaration is another form of using the copy constructor. What is right?

+7
source share
4 answers

When = appears during initialization, it calls the copy constructor. The general view is not quite the same as calling the copy constructor. In the expression, T a = expr; what happens is that if expr is of type T, the copy constructor is called. If expr is not of type T, then an implicit conversion is performed first, if possible, then the copy constructor is called with it as an argument. If implicit conversion is not possible, then the code is poorly formed.

Depending on how the getVector() structure is structured, the copy can be optimized, and the object created inside the function is the same physical object that is stored in v.

+8
source

Assuming you haven’t done something pathological outside the code that you display, your ad is copy initialization, and the second part of this rule applies:

 13.3.1.3 Initialization by constructor [over.match.ctor] 1 When objects of class type are direct-initialized (8.5), or copy-initialized from an expression of the same or a derived class type (8.5), overload resolution selects the constructor. For direct-initialization, the candidate functions are all the constructors of the class of the object being initialized. For copy-initialization, the candidate functions are all the converting constructors (12.3.1) of that class. The argument list is the expression-list within the parentheses of the initializer. 

For a simple test case, see Eli Bendersky's post, here: http://eli.thegreenplace.net/2003/07/23/variable-initialization-in-c/

+2
source

Always remember the rule:
Whenever an object is created and gets some value in the same same expression, it never assigns.

To add further,

Case 1:

 Vector v1; Vector v(v1); 

Case 2:

  Vector v = getVector(); 

In the above two formats, Case 1 is Direct Initialization , and Case 2 is known as Copy Initialization .

How does copy initialization work?
Copy initialization creates an implicit conversion sequence: it tries to convert the return value of getVector() to an object of type Vector . Then it can copy the created object to the initialized object, so it needs an available copy constructor.

+1
source

In this case, the copy constructor is actually deleted (check this ), and only the default constructor finishes getting the called

EDIT:

The constructor is only occasionally deleted, as Benyamine's answer. For some reason, I read this when you call the constructor directly.

0
source

All Articles