Connecting Tech Pros Worldwide Forums | Help | Site Map

copy ctor vs default ctor

subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
#1: Aug 15 '07
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


Gianni Mariani
Guest
 
Posts: n/a
#2: Aug 15 '07

re: copy ctor vs default ctor


subramanian100in@yahoo.com, India wrote:
Quote:
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?=
Guest
 
Posts: n/a
#3: Aug 15 '07

re: copy ctor vs default ctor


On 2007-08-15 08:55, subramanian100in@yahoo.com, India wrote:
Quote:
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
Closed Thread