su**************@yahoo.com, India wrote:
If we provide any ctor for a class the compiler does not supply the
default ctor.
However if we do not provide the copy ctor but provide any other ctor,
the compiler still supplies the copy ctor.
Why doesn't the compiler supply the default ctor but still supplies
the copy ctor when we have defined any other ctor ?
If you provide your own version of a constructor, you have some special
functionality in mind to bring your newly constructed object into a well-defined
state, so as to fulfill some class invariant. Obviously, the compiler generated
default constructor cannot guess this functionality, so it is discarded. As you
stated, this applies only to the default constructor. The rationale behind this
may be, that copying may still be a simple task (copying the object bit for bit
could preserve the class invariant), so even if the standard constructor doesn't
meet your requirements, the copy constructor still may do so.
If you ask me, I'd say that C++ should have some features to describe whether a
compiler generated piece of functionality is desired:
class SomeClass
{
// This could be a way to specify that the default standard constructor
// should be generated.
using SomeClass ();
// This could be a way to specify that the default copy constructor
// should be generated.
using SomeClass (const SomeClass& other);
};
Regards,
Stuart