Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > default ctor

Reply
Thread Tools

default ctor

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      11-26-2007
If we provide a any non-default ctor, then the compiler doesn't
supply the default ctor even if needed. This behaviour is due the
following reason as per my understanding:
It is for DISALLOWING an object to be created without arguments.
For example, for a class named Test, creating
Test obj;
may not have to be allowed for the particular class Test. Under this
circumstance, we expect a provision from the compiler, not to
supply the default ctor.
Is my understanding correct ?

Or is there any other reason for the compiler not providing the
default ctor when we have defined some ctor ?

Kindly clarify.

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      11-26-2007
, India wrote:
> If we provide a any non-default ctor, then the compiler doesn't
> supply the default ctor even if needed. This behaviour is due the
> following reason as per my understanding:
> It is for DISALLOWING an object to be created without arguments.


This needs to be expressed more carefully: It is for disallowing object creation
using a constructor that has been generated by the compiler. Altough this
statement may seem not to be different from the original statement of yours, it
encloses the case where a explicitly declared constructor has default values for
its arguments:
class Test
{
Test (int i = 2);
};
Here we can construct an object without passing arguments but it is still not
the default constructor.

> For example, for a class named Test, creating
> Test obj;
> may not have to be allowed for the particular class Test. Under this
> circumstance, we expect a provision from the compiler, not to
> supply the default ctor.
> Is my understanding correct ?


I very much think so.

Regards,
Stuart
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 8:00 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> If we provide a any non-default ctor, then the compiler doesn't
> supply the default ctor even if needed. This behaviour is due the
> following reason as per my understanding:
> It is for DISALLOWING an object to be created without arguments.
> For example, for a class named Test, creating
> Test obj;
> may not have to be allowed for the particular class Test. Under this
> circumstance, we expect a provision from the compiler, not to
> supply the default ctor.
> Is my understanding correct ?


Not really. The compiler supplied default constructor is more
or less a no-op. Presumably, if you provide a constructor (any
constructor), then a no-op is not an appropriate initialization,
so the compiler won't generate one.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 12:49 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 26, 8:00 am, "subramanian10...@yahoo.com, India"
>
> <subramanian10...@yahoo.com> wrote:
> > If we provide a any non-default ctor, then the compiler doesn't
> > supply the default ctor even if needed. This behaviour is due the
> > following reason as per my understanding:
> > It is for DISALLOWING an object to be created without arguments.
> > For example, for a class named Test, creating
> > Test obj;
> > may not have to be allowed for the particular class Test. Under this
> > circumstance, we expect a provision from the compiler, not to
> > supply the default ctor.
> > Is my understanding correct ?

>
> Not really. The compiler supplied default constructor is more
> or less a no-op.


Not when data members and base classes declare none-trivial default
ctors.

> Presumably, if you provide a constructor (any
> constructor), then a no-op is not an appropriate initialization,
> so the compiler won't generate one.


If you provide a constructor (any constructor), then a compiler
generated default constructor might bring about rational errors.So you
must decide whether or not to provide a default ctor and accept the
responsability yourself.

regards,
FM.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-26-2007
terminator wrote:

>
> Not when data members and base classes declare none-trivial default
> ctors.


Actually, even more so in that case. If the data members have explicit
default constructors, then the constructor the user provides
T() { }
to replace the implicitly generated one, is very trivial.
The only time it's not trivial is where the default constructors of
the sub classes and members do not have the proper initialization
behavior.


Where the wheels come off is the inane C++ concept of the
"we don't always initialize POD types" behavior. The value
initializaiton is a half-assed attempt to reign in this
behavior without addressing the underlying problem.
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 3:55 pm, Ron Natalie <r...@spamcop.net> wrote:
> terminator wrote:
>
> > Not when data members and base classes declare none-trivial default
> > ctors.

>
> Actually, even more so in that case. If the data members have explicit
> default constructors, then the constructor the user provides
> T() { }


Not for all cases:just take STL classes : zeroing is not 'no op'.

regards,
FM.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-26-2007
terminator wrote:
> On Nov 26, 3:55 pm, Ron Natalie <r...@spamcop.net> wrote:
>> terminator wrote:
>>
>>> Not when data members and base classes declare none-trivial default
>>> ctors.

>> Actually, even more so in that case. If the data members have explicit
>> default constructors, then the constructor the user provides
>> T() { }

>
> Not for all cases:just take STL classes : zeroing is not 'no op'.
>
> regards,
> FM.

I have no clue what you are talking about. But STL or class or
otherwise, if all the subobject have proper default constructors,
thats all the default constructor need do (and it is identical
to the one the compiler would generate).
 
Reply With Quote
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      11-27-2007
On Nov 26, 2:49 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Nov 26, 8:00 am, "subramanian10...@yahoo.com, India"
>
> <subramanian10...@yahoo.com> wrote:
> > If we provide a any non-default ctor, then the compiler doesn't
> > supply the default ctor even if needed. This behaviour is due the
> > following reason as per my understanding:
> > It is for DISALLOWING an object to be created without arguments.
> > For example, for a class named Test, creating
> > Test obj;
> > may not have to be allowed for the particular class Test. Under this
> > circumstance, we expect a provision from the compiler, not to
> > supply the default ctor.
> > Is my understanding correct ?

>
> Not really. The compiler supplied default constructor is more
> or less a no-op. Presumably, if you provide a constructor (any
> constructor), then a no-op is not an appropriate initialization,
> so the compiler won't generate one.
>
> --
> James Kanze (GABI Software)


Then shouldn't the same reasoning be applied to the
copy ctor ? ie Take the copy ctor for example. Suppose we
have provided some ctor and not the copy ctor. Then the
compiler shouldn't supply the copy ctor too, even if it
is needed. But it provides the copy ctor. What is the
difference between the two scenarios ?

I am unable to understand.

Kindly clarify my doubt.

Please excuse me for asking it again.

Thanks
V.Subramanian
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-27-2007
On Nov 27, 7:32 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> On Nov 26, 2:49 pm, James Kanze <james.ka...@gmail.com> wrote:
> > On Nov 26, 8:00 am, "subramanian10...@yahoo.com, India"


> > <subramanian10...@yahoo.com> wrote:
> > > If we provide a any non-default ctor, then the compiler doesn't
> > > supply the default ctor even if needed. This behaviour is due the
> > > following reason as per my understanding:
> > > It is for DISALLOWING an object to be created without arguments.
> > > For example, for a class named Test, creating
> > > Test obj;
> > > may not have to be allowed for the particular class Test. Under this
> > > circumstance, we expect a provision from the compiler, not to
> > > supply the default ctor.
> > > Is my understanding correct ?


> > Not really. The compiler supplied default constructor is
> > more or less a no-op. Presumably, if you provide a
> > constructor (any constructor), then a no-op is not an
> > appropriate initialization, so the compiler won't generate
> > one.


As has been pointed out, this is a (intentional) simplification.
The rationale here really does concern simple objects, made up
of basic types.

> Then shouldn't the same reasoning be applied to the
> copy ctor ?


Not necessarily, because the copy constructor copies everything.
It's never a no-op, so all elements of the target are
initialized.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      11-27-2007
On Nov 26, 5:39 pm, Ron Natalie <r...@spamcop.net> wrote:
> terminator wrote:
> > On Nov 26, 3:55 pm, Ron Natalie <r...@spamcop.net> wrote:
> >> terminator wrote:

>
> >>> Not when data members and base classes declare none-trivial default
> >>> ctors.
> >> Actually, even more so in that case. If the data members have explicit
> >> default constructors, then the constructor the user provides
> >> T() { }

>
> > Not for all cases:just take STL classes : zeroing is not 'no op'.

>
> > regards,
> > FM.

>
> I have no clue what you are talking about. But STL or class or
> otherwise, if all the subobject have proper default constructors,
> thats all the default constructor need do (and it is identical
> to the one the compiler would generate).


Do I have to declare a default constructor that does not do anything?
Vector provides a default ctor that zeros the size and probably the
capacity,so it is not 'no op'.

regards,
FM.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments