Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

c++ - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...