Calling Inherited Constructors

Discussion in 'C++' started by Justin, Nov 3, 2005.

  1. Justin

    Justin Guest

    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
     
    Justin, Nov 3, 2005
    #1
    1. Advertising

  2. > 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...
     
    Peter Steiner, Nov 3, 2005
    #2
    1. Advertising

  3. 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
     
    Karl Heinz Buchegger, Nov 3, 2005
    #3
  4. Justin

    Ed Guest

    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
    >
     
    Ed, Nov 3, 2005
    #4
  5. Justin

    Justin Guest

    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
     
    Justin, Nov 3, 2005
    #5
  6. Justin

    Justin Guest

    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
     
    Justin, Nov 3, 2005
    #6
  7. Justin

    Kaz Kylheku Guest

    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.

    :)
     
    Kaz Kylheku, Nov 4, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chris sennitt

    constructors not inherited ?

    chris sennitt, Mar 6, 2004, in forum: Java
    Replies:
    8
    Views:
    11,783
    Tor Iver Wilhelmsen
    Mar 6, 2004
  2. Brian Genisio

    inherited constructors

    Brian Genisio, Jan 5, 2004, in forum: C++
    Replies:
    10
    Views:
    8,294
    jeffc
    Jan 9, 2004
  3. Replies:
    20
    Views:
    773
    Gabriel
    Dec 1, 2005
  4. Joe, G.I.

    calling inherited constructors

    Joe, G.I., Sep 22, 2008, in forum: C++
    Replies:
    2
    Views:
    323
    Andre Kostur
    Sep 23, 2008
  5. 7stud --
    Replies:
    11
    Views:
    411
    7stud --
    Nov 9, 2007
Loading...

Share This Page