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

77,719

Solution 1

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.

Solution 2

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.

Solution 3

There is no such thing as a default copy constructor. There are default constructors and copy constructors and they are different things.

The implicitly defined copy constructor (which I think is what you mean by "default copy constructor") will copy non-static members of class type using their copy constructor, not their default constructor. The implicitly defined copy constructor is used when you don't define your own copy constructor.

Share:
77,719
sourabh912
Author by

sourabh912

Updated on July 09, 2022

Comments

  • sourabh912
    sourabh912 almost 2 years

    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?

  • Nim
    Nim almost 12 years
    I don't understand your last statement - can you clean this answer up?
  • Christian Rau
    Christian Rau almost 12 years
    Are you sure about your first paragraph? See my comments on AIs' answer.
  • NullPoiиteя
    NullPoiиteя almost 12 years
  • Christian Rau
    Christian Rau almost 12 years
    Yes, and it's saying nothing about copy constructors. Only that default constructors are not generated if any constructors are present. I see that you might have misinterpreted the OP's term "default copy constructor" to mean default constructor (like others seem to have done, too), but on the other hand I cannot see how not to understand this as implicitly generated copy constructor. And for copy constructors your first paragraph is plain wrong, since they are always generated if no copy constructor is present, like you even say in your last paragraph.
  • P.P
    P.P almost 12 years
    @ChristianRau You better ask the original author. Paragraph 2 and 3
  • sourabh912
    sourabh912 almost 12 years
    @James Kanze: Very good explaination , by the default copy constructor I meant implicitly defined by the compiler.
  • David Hammen
    David Hammen almost 12 years
    Agreed. +1. A "default copy constructor" is a copy constructor that takes a default, nice! That raises an interesting question: How to construct ourDefaultInstance. Default constructing it would be UB. I suppose a non-default constructor is needed.
  • James Kanze
    James Kanze almost 12 years
    @ChristianRau It depends on what he's trying to say in the first paragraph. Once you define any constructor for a class, the default constructor is no longer generated. Is this what he means (where "all the default ones" means "the default constructor", and "becomes unavailable" means "there is no implicitly declared one")? Or is he making the same confusion he explains in the second paragraph.
  • Shane
    Shane almost 12 years
    Nim, by default, the compiler provides a copy constructor. For some instances you may wish to define your own copy constructor, reasons such as only copying one or two data members over out of 10 (for example), or for code optimization. If you define your own copy constructor, the compiler will not give the class a default copy constructor. It is also possible to completely remove the copy constructor by means of defining it (I am not sure on this process), to ensure it is not available. Hope that clears things up?
  • Christian Rau
    Christian Rau almost 12 years
    @KingsIndian Hah, nice find. In fact the original author caused confusion, too.
  • James Kanze
    James Kanze almost 12 years
    @DavidHammen Yes. I only showed the minimum essential to the point I was making, but anytime you declare a copy constructor, you must declare other constructors as well, or you'll never be able to construct anything to copy. In this case, yes, trying to default construct ourDefaultInstance would be undefined behavior.
  • Christian Rau
    Christian Rau almost 12 years
    @JamesKanze He is likely making the same confusion as the OP when he says "...all the default ones..." (since otherwise there is just one single one) and "...non-default (ie explicit)...". Even though in the second paragraph he objects from it and in the third he makes the correct statement about the copy constructor. In fact KingsIndian's finds make me think he hasn't though that much about his own answer, but who knows.
  • James Kanze
    James Kanze almost 12 years
    @DavidHammen A "default copy constructor" is a default constructor (can be called with no arguments) and a copy constructor (can be called with one argument of the same type). You can throw in as many additional parameters as you want, as long as all of them have default arguments.
  • MSalters
    MSalters almost 12 years
    @Shane: The latter isn't true. You can remove it, yes, but only in C++11. Also, it's not done by defining it, but by declaring it as =delete.
  • Shane
    Shane almost 12 years
    @MSalters: Ah, that sounds right. As I said, unsure about the process, thanks for clearing that up for me.
  • James Kanze
    James Kanze almost 12 years
    @ChristianRau Without his second paragraph, I'd be pretty sure that he's making the same confusion as the OP. With it, I can only agree with your last three words.
  • j0rre
    j0rre about 10 years
    Just stumbled over this. Regarding the last sentence in the answer: According to this reference, in C++ 11 generation of the implicit copy constructor can be forced by using the keyword default, despite the existence of a user defined copy constructor.
  • Aaron McDaid
    Aaron McDaid over 9 years
    @MSalters, pre C++11, you can declare a private copy constructor to stop others using it. Or, even just declare it but without an implementation! T(const T&);