default and copy ctors

S

subramanian100in

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 ?

Kindly explain

Thanks
V.Subramanian
 
S

Stuart Redmann

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
 
?

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

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 ?

Kindly explain

Because usually you want a copy ctor, and with the behaviour of the
default one. It's quite usual that you need to supply your own ctor but
that the default copy ctor is good enough so it simply saves time, same
as the fact that you get a default operator=, it would take too much
time if you'd have to write them all the time. Consider the case of a
simple struct:

struct Test {
int foo;
int bar;
double baz;
};

It might be nice to have a ctor so you can create and initialize a new
instance at the same time, but you probably don't want to change the
behaviour of the default copy ctor or operator=.
 
?

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

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);
};

In a way you can get this functionality, except you have to specify
which ones you don't want instead of which you want. Just declare any
you don't want private.
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top