Calling Inherited Constructors

J

Justin

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
 
P

Peter Steiner

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...
 
K

Karl Heinz Buchegger

Justin said:
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;
}
 
E

Ed

You should make the foo member function static thus avoiding any
possibility of doing anything that is undefined.
 
J

Justin

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
 
J

Justin

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
 
K

Kaz Kylheku

Justin said:
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.

:)
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top