By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

Calling Inherited Constructors

P: n/a
Hello, first time posting.
If I have a base class and a derived class, is there only one way to
call the base constructor?

i.e. Is this the only way I can call the base constructor (presuming
the base constructor took an int):

Derived::Derived(int a) : Base(a)
{ ... }

I know the base class must be constructed before the derived class
is..so I guess my question is, is there any way that I can run some
code in the derived constructor before calling the base constructor?

Thanks for any help,
Justin

Nov 3 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> I know the base class must be constructed before the derived class
is..so I guess my question is, is there any way that I can run some
code in the derived constructor before calling the base constructor?


no, there is no way to do what you in intend with constructors.

a solution would be to move initialization in the base class to a
protected method, which you can then call in the derived class ctor
after you ran your code there.

this doesn't work though if the code in the derived class is to be
executed before any parent class instance members are initialized, as
this already happens in the parent ctor. then you would have to delay
initializing these too by not storing by value...

Nov 3 '05 #2

P: n/a
Justin wrote:

Hello, first time posting.

If I have a base class and a derived class, is there only one way to
call the base constructor?

i.e. Is this the only way I can call the base constructor (presuming
the base constructor took an int):

Derived::Derived(int a) : Base(a)
{ ... }
I know the base class must be constructed before the derived class
is..so I guess my question is, is there any way that I can run some
code in the derived constructor before calling the base constructor?


well. not exactly.
But a there is a small gap in the door :-)

Derived::Derived(int a) : Base( foo( a ) )
{
}

int Derived::foo( int arg )
{
// now you can do whatever you want
// before the base ctor gets called

// but beware
// the Derived class object has not been fully constructed yet,
// so be careful in what you do

return arg;
}
--
Karl Heinz Buchegger
kb******@gascad.at
Nov 3 '05 #3

P: n/a
Ed
You should make the foo member function static thus avoiding any
possibility of doing anything that is undefined.

Ed
Karl Heinz Buchegger wrote:
Justin wrote:

Hello, first time posting.

If I have a base class and a derived class, is there only one way to
call the base constructor?

i.e. Is this the only way I can call the base constructor (presuming
the base constructor took an int):

Derived::Derived(int a) : Base(a)
{ ... }

I know the base class must be constructed before the derived class
is..so I guess my question is, is there any way that I can run some
code in the derived constructor before calling the base constructor?


well. not exactly.
But a there is a small gap in the door :-)

Derived::Derived(int a) : Base( foo( a ) )
{
}

int Derived::foo( int arg )
{
// now you can do whatever you want
// before the base ctor gets called

// but beware
// the Derived class object has not been fully constructed yet,
// so be careful in what you do

return arg;
}
--
Karl Heinz Buchegger
kb******@gascad.at


Nov 3 '05 #4

P: n/a
Aha! Thank you Karl, that is exactly what I needed. Don't worry, I
wasn't intending to do anything with the partially constructed base
class at all. The base class was originally designed to be declared
and used exclusively as private member data for a class that used it as
an internal data type, but since I have pulled it out to use it
independently. The issue arose when I pulled it from the outer class
that contained code in its constructor to parse an XML file and open
some files..etc.

Thanks again

Nov 3 '05 #5

P: n/a
Aha! Thank you Karl, that is exactly what I needed. Don't worry, I
wasn't intending to do anything with the partially constructed base
class at all. The base class was originally designed to be declared
and used exclusively as private member data for a class that used it as
an internal data type, but since I have pulled it out to use it
independently. The issue arose when I pulled it from the outer class
that contained code in its constructor to parse an XML file and open
some files..etc.

Thanks again

Nov 3 '05 #6

P: n/a
Justin wrote:
Hello, first time posting.

If I have a base class and a derived class, is there only one way to
call the base constructor?
No. Constructors can be overloaded. The base can have many possible
constructors for you to call. If you don't specify one, then the
default one is used.

If there isn't a default one, and your constructor doesn't call it from
its initializer list, then you have an error.
i.e. Is this the only way I can call the base constructor (presuming
the base constructor took an int):

Derived::Derived(int a) : Base(a)
{ ... }
This is the only way you can call /that/ base class constructor, yes.
I know the base class must be constructed before the derived class
is..so I guess my question is, is there any way that I can run some
code in the derived constructor before calling the base constructor?


No. You can only call base constructors in the base and member
initializer list.

Moreover, the order of appearance of expressions in that list isn't
necessarily the order in which they are evaluated. Be careful! The list
is a specification of what component gets what initialization. It is
not a sequential program.

If you call a constructor in an ordinary statement body, what happens
is that it returns a temporary object. A constructor is not invoked on
the ``this'' object.

By the time you run the constructor body, the base class parts are
already constructed. You can adjust the base class object by assigning
to its members, or calling additional routines in the base class
(two-step initialization).

There is also a way to call a constuctor on some given piece of memory.
This is done by the so called ``placement new'' operator.

There is a trick you can do to re-initialize a base class differently.
You do a in-place destructor call to wipe out the base, and then a
placement new over it.

I would put this in the category of "black art", and would never do it
over anything other than bases which are POD's (plain old datatypes).
I'm pretty sure the behavior is undefined, since basically we are
destroying an object midway through a construction, re-constructing
that destroyed part of it and then depending on the remainder of the
construction going fine to build the derived parts on top of the
reconstructed bases.

:)

Nov 4 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.