Are there any differences between the two lines?

We can create an object in two ways:

myClass myObject = myClass(123); //or myClass myObject(123); 

Are there any differences in background between the two? I want to use the first one, but it looks like they combine these two lines:

 myClass myObject; myObject= myClass(123); 

Does he do the same second?

+6
source share
3 answers
 myClass myVariable = myClass(123); 

is copy initialization .

 myClass myVariable(123); 

- direct initialization .

 myClass myVariable; myVariable = myClass(123); 

is the default initialization , and then copy (or move) .

Typically, the first two are identical due to copy elision . The corresponding rule can be found in [class.copy] / 31 (N4140, C ++ 14 draft standard):

When certain criteria are met, the implementation may skip copying / moving the class object [...]:

- when an object of a temporary class that is not attached to a link (12.2) is copied / transferred to a class object with the same cv-unqualified type, the copy / move operation can be omitted from building the temporary object directly to the omitted copy / move target

+10
source

Not that I knew, after all. You have described three ways to define an object, and all 3 paths place the object on top of the stack, invoking the same constructor. Functionality would be different if you used the new operator.

+1
source

This operator does not use an assignment operator (two lines are equivalent, as far as I know). = used in the syntax, but operator= is not actually used:

 myClass myVariable = myClass(123); //or myClass myVariable(123); 

In this case, the assignment operator is used:

 myClass myVariable; myVariable = myClass(123); 

If the assignment operator is poor or not implemented, the first operator works, the second may (and most likely) fail.

 #include <iostream> #include <string.h> using namespace std; class Dvector { public: Dvector( int thesize = 0 ) { std::cout << "Constructing object of size " << thesize << std::endl; size = thesize; data = new double[size]; } Dvector( const Dvector& v ) { std::cout << "Constructing object of size " << v.size << " by copy" << std::endl; size = v.size; data = new double[size]; memcpy( data, v.data, sizeof(double)*size ); } Dvector& operator=( const Dvector& v ) { std::cout << "Assigning object of size " << v.size << std::endl; if ( &v != this ) { size = v.size; data = new double[size]; memcpy( data, v.data, sizeof(double)*size ); } return *this; } ~Dvector() { std::cout << "Destroying object" << std::endl; delete [] data; } private: double* data; int size; }; int main() { Dvector v = Dvector(3); return 0; } 

Output:

 Constructing object of size 3 Destroying object 

When:

 int main() { Dvector v; v = Dvector(3); return 0; } 

Output:

 Constructing object of size 0 Constructing object of size 3 Assigning object of size 3 Destroying object Destroying object 

And it would crash if the copy constructor were not defined ... because then v.data ends up pointing to data allocated by temporary variables ( Dvector(3) ) and then is deleted. Possible failure when trying to access v.data or when v destroyed (deletion of already freed memory).

+1
source

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


All Articles