Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I defined a non-copy constructor; will a copy constructor still be implicitly defined?

Can the (implicit)default copy constructor be called for a class that has already user-defined constructor but that is not the copy constructor?

If it is possible then, suppose we define the copy constructor for the class explicitly, now can the (implicit)default constructor be called?

like image 910
sourabh912 Avatar asked Sep 25 '12 07:09

sourabh912


People also ask

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.

What happens if a copy constructor is not defined?

If we don't define our own copy constructor, the C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects. The compiler-created copy constructor works fine in general.

Can constructor be called implicitly?

A constructor is a special member function that is automatically called by the compiler when an object is created and the destructor is also a special member function that is also implicitly called by the compiler when the object goes out of scope.

When copy constructor is used implicitly for what purpose?

A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written (see Rule of three).


2 Answers

First, let's clarify our vocabulary a bit. A default constructor is a constructor which can be called without any arguments. A copy constructor is a constructor which can be called with a single argument of the same type. Given this, a "default copy constructor" would be a constructor with a signature something like:

class MyClass { public:     static MyClass ourDefaultInstance;     //  default copy constructor...     MyClass( MyClass const& other = ourDefaultInstance ); }; 

Somehow, I don't think that this is what you meant. I think what you're asking about is an implicitly declared or an implicitly defined copy constructor; a copy constructor whose declaration or definition is provided implicitly by the compiler. The compiler will always provide the declaration unless you provide a declaration of something that can be considered a copy constructor. Providing other constructors will not prevent the compiler from implicitly declaring a copy constructor.

This is different from the default constructor—any user defined constructor will prevent the compiler from implicitly declaring a default constructor. This means that if you have a user defined copy constructor, the compiler will not implicitly declare a default constructor.

The second important point is that you do not call constructors. The compiler calls them in certain well defined contexts: variable definition and type conversion, mainly. The compiler can only call constructors that are declared (including those that are implicitly declared). So if you have a user defined constructor (copy or otherwise), and do not define a default constructor, the compiler cannot call the constructor except in contexts where it has arguments to call it with.

To summarize what I think your questions are: the compiler will provide an implicit copy constructor even if the class has other user defined constructors, provided none of those constructors can be considered copy constructors. And if you provide a user defined copy constructor, the compiler will not provide an implicitly declared default copy constructor.

like image 76
James Kanze Avatar answered Sep 16 '22 12:09

James Kanze


http://www.cplusplus.com/articles/y8hv0pDG/

The default copy constructor exists if you have not defined one. So yes you can call the default copy constructor, if you haven't defined a copy constructor, however if you do define a copy constructor in your class, you will not be able to call the default one.

like image 23
CrazyCasta Avatar answered Sep 20 '22 12:09

CrazyCasta