By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,516 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,516 IT Pros & Developers. It's quick & easy.

copy ctor vs default ctor

P: n/a
If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.

Kindly explain

Thanks
V.Subramanian

Aug 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
su**************@yahoo.com, India wrote:
If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.
The C++ standard specifies it that way. I can only imagine that the C++
standard was specified this way because it's more likely that if a
default CTOR is not provided while others are, you more than likely
don't want a default ctor while you do want a copy ctor.
Aug 15 '07 #2

P: n/a
On 2007-08-15 08:55, su**************@yahoo.com, India wrote:
If we do not provide any ctor for a class, the compiler provides the
default ctor and copy ctor if needed.

Consider a class Test.
Suppose we provide some ctor in class Test but do not provide the
default ctor.

Suppose we try to create

Test obj;

Here the default ctor is needed and the compiler does not provide the
default ctor but generates a compilation error.

However if we provide some ctor but do not provide the copy ctor, the
compiler does not generate error but provides the copy ctor if needed.

Why doesn't the compiler give error for the absence of copy ctor
similar to error for the absence of default ctor.
Because the generation of default ctor and copy ctor are independent of
each other. If you declare any ctor (even a copy ctor) the compiler wont
generate a default ctor. If you declare a copy ctor the compiler wont
generate a default copy ctor, nor will it generate a default ctor.

--
Erik Wikström
Aug 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.