reg constructors/copy constructors inheritance

S

srp113

Hello,
I have a base class A, and class D derived from A. I declare a
constructor(ctr) for B() but in definition I dont explicitly call
constructor for A. Compiler however seems to take care of this and
when I construct an object of D, it calls A's ctr first followed by
D's ctr. I tried to see if same holds true for copy constructors: I
defined copy constructor for A and D, and D's copy ctr didnt include
any calls for calling A's copy ctr. In this case though, what happens
is A's default ctr (NOT copy ctr) is called and then B's copy ctr. Why
this discrepancy? In general if you define a constructor (default/
copy) in derived class is it better to include an explicit call to
corr. base classes constructor?
Thanks,
Sunil
 
A

Alf P. Steinbach

* srp113:
Hello,
I have a base class A, and class D derived from A. I declare a
constructor(ctr) for B() but in definition I dont explicitly call
constructor for A. Compiler however seems to take care of this and
when I construct an object of D, it calls A's ctr first followed by
D's ctr. I tried to see if same holds true for copy constructors: I
defined copy constructor for A and D, and D's copy ctr didnt include
any calls for calling A's copy ctr. In this case though, what happens
is A's default ctr (NOT copy ctr) is called and then B's copy ctr. Why
this discrepancy?

For the question of "why" you'd better ask in [comp.std.c++].

But regarding what the rules are, not why the are: the automatically generated
calls do not depend on the arguments to the constructor in question.

Copy constructors are treated specially in some ways, namely (1) that a copy
constructor can be automatically generated, (2) that a templated constructor is
never a copy constructor (i.e. you can still get an automatically generated one)
and (3) that copy constructor calls can be elided in certain cases even when the
copy constructor has some side effect, i.e. that the compiler is free to treat a
copy constructor as if it really just copied and nothing else.

But copy constructors are not treated specially with regard to initialization of
bases and members.

So, if you define a copy constructor without using an initializer list to call
base class and member copy constructors, those will default-initialized -- and
if they cannot be default-initialized the compiler will protest very loudly.

In general if you define a constructor (default/
copy) in derived class is it better to include an explicit call to
corr. base classes constructor?

For a copy constructor: yes, assuming that by "call" you mean to call it via an
initializer list, like

struct D: A
{
D( D const& other ): A( other ) { ... }
};


Cheers & hth.,

- Alf


PS: I think you meant to write either just "D" or just "B".
 
J

James Kanze

I have a base class A, and class D derived from A. I declare
a constructor(ctr) for B() but in definition I dont explicitly
call constructor for A. Compiler however seems to take care of
this and when I construct an object of D, it calls A's ctr
first followed by D's ctr. I tried to see if same holds true
for copy constructors: I defined copy constructor for A and D,
and D's copy ctr didnt include any calls for calling A's copy
ctr. In this case though, what happens is A's default ctr (NOT
copy ctr) is called and then B's copy ctr. Why this
discrepancy?

There's no discrepancy. If a base class has a constructor (of
any type), then all derived class constructors will call a
constructor of the base class. If you don't specify arguments
for it in the initialization list, it will call the default
constructor for the base class; the one without arguments. A
copy constructor is no exception to this rule. (Obviously, in
the case of a copy constructor, this is rarely what we want, but
it's still up to you to define something else.)

(Copy constructors are a bit special in other ways: if you don't
declare one, the compiler will, and if the compiler declared
copy constructor is used, the compiler will also define it. And
the compiler defined copy constructor copies all of the base
classes and members---using their copy constructors.)
In general if you define a constructor (default/ copy) in
derived class is it better to include an explicit call to
corr. base classes constructor?

It depends. If I want the base class default constructed, I'll
usually just skip it, unless the base class is a POD. But
anytime you want anything other than default construction, you
have to explicitly mention the class in the initialization list.
 
G

gob00st

There's no discrepancy.  If a base class has a constructor (of
any type), then all derived class constructors will call a
constructor of the base class.  If you don't specify arguments
for it in the initialization list, it will call the default
constructor for the base class; the one without arguments.  A
copy constructor is no exception to this rule.  (Obviously, in
the case of a copy constructor, this is rarely what we want, but
it's still up to you to define something else.)

(Copy constructors are a bit special in other ways: if you don't
declare one, the compiler will, and if the compiler declared
copy constructor is used, the compiler will also define it.  And
the compiler defined copy constructor copies all of the base
classes and members---using their copy constructors.)


It depends.  If I want the base class default constructed, I'll
usually just skip it, unless the base class is a POD.  But
anytime you want anything other than default construction, you
have to explicitly mention the class in the initialization list.

--
James Kanze (GABI Software)             email:[email protected]
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

There is no discrepancy between compile defined constructor and
compile defined copy constructor,
which calls its corresponding base counterpart.

But there is no discrepancy between user defined constructor and user
defined copy constructor(both without initialization list),which calls
base class cotr. (Would it make more sense to make user defined copy
cotr without initialization list to call its base class copy cotr in
the standard?)

Gob00st
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top