copy ctor vs default ctor

S

subramanian100in

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
 
G

Gianni Mariani

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top