42

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?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
sourabh912
  • 669
  • 3
  • 7
  • 14
  • http://stackoverflow.com/questions/10854948/when-is-default-constructor-generated-in-c – BoBTFish Sep 25 '12 at 07:15
  • Copy constructor and constructor are different and they called in different situation, one doesn't hide another. – MarsRover Sep 25 '12 at 07:16
  • The question is, why would you want to do this. Usually you define your own copy-constructor because the default one is not sufficient - then why would you want to call it? – Björn Pollex Sep 25 '12 at 07:16
  • Having the default and the overridden copy constructors at the same time is ambiguous. – Mark Garcia Sep 25 '12 at 07:18

3 Answers3

110

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.

Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 5
    @James Kanze: Very good explaination , by the default copy constructor I meant implicitly defined by the compiler. – sourabh912 Sep 25 '12 at 08:06
  • 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. – David Hammen Sep 25 '12 at 08:07
  • @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. – James Kanze Sep 25 '12 at 08:12
  • @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. – James Kanze Sep 25 '12 at 08:15
  • 4
    Just stumbled over this. Regarding the last sentence in the answer: According to [this](http://en.cppreference.com/w/cpp/language/copy_constructor) 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. – j0rre Apr 05 '14 at 21:00
14

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.

Shane
  • 2,007
  • 18
  • 33
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • 2
    I don't understand your last statement - can you clean this answer up? – Nim Sep 25 '12 at 07:16
  • 1
    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? – Shane Sep 25 '12 at 08:09
  • 1
    @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`. – MSalters Sep 25 '12 at 08:20
  • @MSalters: Ah, that sounds right. As I said, unsure about the process, thanks for clearing that up for me. – Shane Sep 25 '12 at 08:23
  • 1
    @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&);` – Aaron McDaid Dec 01 '14 at 16:45
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.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Are you sure about your first paragraph? See my comments on *AIs*' answer. – Christian Rau Sep 25 '12 at 07:42
  • @ChristianRau check this article http://codewrangler.home.comcast.net/~codewrangler/tech_info/ctor_init.html – NullPoiиteя Sep 25 '12 at 07:52
  • 1
    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. – Christian Rau Sep 25 '12 at 07:58
  • @ChristianRau You better ask the [original author](http://stackoverflow.com/a/1585713/1275169). [Paragraph 2 and 3](http://www.velocityreviews.com/forums/t279390-c-default-copy-constructor.html) – P.P Sep 25 '12 at 08:03
  • @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. – James Kanze Sep 25 '12 at 08:09
  • @KingsIndian Hah, nice find. In fact the original author caused confusion, too. – Christian Rau Sep 25 '12 at 08:10
  • @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. – Christian Rau Sep 25 '12 at 08:13
  • @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. – James Kanze Sep 25 '12 at 08:58