Topic: question on copy ctor
Author: "subramanian100in@yahoo.com, India" <subramanian100in@yahoo.com>
Date: Tue, 14 Aug 2007 07:10:59 CST Raw View
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 an 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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "terminator(jam)" <farid.mehrabi@gmail.com>
Date: Tue, 14 Aug 2007 13:15:32 CST Raw View
On Aug 14, 4:10 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> 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 an 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
>
Very interesting!! I think if things were as you mentioned,then sb
would complaign.
There are many cases where you just need a memberwise copy,while
there are many other case where you need a more complex copy ctor.the
designers have decide not to bother themselves as well as programmers
to force any class with a ctor to declare a copy ctor.
more over when you declare a ctor and need a trivial default that only
calls subobjects` default ctors you can simply define it via empty
braces{}( no need for an initializer list).But with copy, you would
have to copy construct every subobject explicitly in the initializer
list otherwise the semantics for copy ctor initializer lists would
have to be different from those of other ctors which would in turn
increase the risk of logical errors and the difficulty of tracing
ctor related mistakes .
regards,
FM.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]