Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ constructor syntax [duplicate]

Simple question: are the following statements equivalent? or is the second one doing more implicit things behind the scenes (if so, what?)

myClass x(3);
myClass x = myClass(3);

Thanks!

like image 410
Steve Avatar asked Feb 25 '09 17:02

Steve


People also ask

What is the syntax of copy constructor?

Syntax Of User-defined Copy Constructor:{ A(A &x) // copy constructor. { // copyconstructor.

Can we copy the constructor?

The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. 2. Copy constructor takes a reference to an object of the same class as an argument.

Can constructor be called twice?

No, not at all. You can't call a constructor more than once on an object. Constructor is invoked (called) only when an object is created using new keyword.

Can you have multiple copy constructors?

A class can have multiple copy constructors, e.g. both T::T(const T&) and T::T(T&). If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default .


1 Answers

They are not completely identical. The first is called "direct initialization" while the second is called "copy initialization".

Now, the Standard makes up two rules. The first is for direct initialization and for copy initialization where the initializer is of the type of the initialized object. The second rule is for copy initialization in other cases.

So, from that point of view both are termed in one - the first - rule. In the case where you have copy initialization with the same type, the compiler is allowed to elide a copy, so it can construct the temporary you create directly into the initialized object. So you can end up very well with the same code generated. But the copy constructor, even if the copy is elided (optimized out), must still be available. I.e if you have a private copy constructor, that code is invalid if the code in which it appears has no access to it.

The second is called copy-initialization, because if the type of the initializer is of a different type, a temporary object is created in trying to implicitly convert the right side to the left side:

myclass c = 3;

The compiler creates a temporary object of the type of myclass then when there is a constructor that takes an int. Then it initializes the object with that temporary. Also in this case, the temporary created can be created directly in the initialized object. You can follow these steps by printing messages in constructors / destructors of your class and using the option -fno-elide-constructors for GCC. It does not try to elide copies then.

On a side-note, that code above has nothing to do with an assignment operator. In both cases, what happens is an initialization.

like image 168
Johannes Schaub - litb Avatar answered Oct 27 '22 13:10

Johannes Schaub - litb