Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call a copy constructor explicitly?

Tags:

People also ask

How do you call a copy constructor explicitly?

If a method takes a reference to an object as a parameter, and the class defines a copy construtor, then the class uses the constructor to create a copy of itself and that gets passed to the function instead of a reference to the original object? Object * obj = new Object(&anotherObject);

What is the need of explicit copy constructor?

Explicit copy constructor It is used to prevent copying of objects at function calls or with the copy-initialization syntax.

When copy constructor is implicitly called?

A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object.

Can I call constructor from copy constructor?

The answer is No. The creation of the object memory is done via the new instruction. Copy constructor is then in charge of the actual copying (relevant only when it's not a shallow copy, obviously). You can, if you want, explicitly call a different constructor prior to the copy constructor execution.


I'm a little confused as to the mechanics of the copy constructor. Correct me if I'm wrong:

If a method takes a reference to an object as a parameter, and the class defines a copy construtor, then the class uses the constructor to create a copy of itself and that gets passed to the function instead of a reference to the original object?

Furthermore, one can call

Object * obj = new Object(&anotherObject);

to create a copy of anotherObject?