call base class constructor from derived class constructor

Discussion in 'C++' started by Rahul, Nov 5, 2007.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    While working with Java, i came across super() which passes values to
    base class constructor from derived class constructor. I was wondering
    if this could be implemented in c++ by any mechanism as super is not
    supported by c++, atleast by MS vc++ 6.0.
     
    Rahul, Nov 5, 2007
    #1
    1. Advertising

  2. Rahul

    Mike Wahler Guest

    "Rahul" <> wrote in message
    news:...
    > Hi Everyone,
    >
    > While working with Java, i came across super() which passes values to
    > base class constructor from derived class constructor. I was wondering
    > if this could be implemented in c++ by any mechanism as super is not
    > supported by c++, atleast by MS vc++ 6.0.


    #include <iostream>

    class Base
    {
    int i;
    public:
    Base(int arg_b = 0) : i(arg_b)
    {
    std::cout << "Base constructor: i = " << i << '\n';
    }
    };

    class Derived : public Base
    {
    public:
    Derived(int arg_d) : Base(arg_d)
    {
    }
    };

    int main()
    {
    Derived d(42);
    return 0;
    }


    Output:
    Base constructor: i = 42


    -Mike
     
    Mike Wahler, Nov 5, 2007
    #2
    1. Advertising

  3. On Nov 5, 3:12 am, Rahul <> wrote:
    > Hi Everyone,
    >
    > While working with Java, i came across super() which passes values to
    > base class constructor from derived class constructor. I was wondering
    > if this could be implemented in c++ by any mechanism as super is not
    > supported by c++, atleast by MS vc++ 6.0.


    Hi Rahul

    C++ doesn't have super keyword. Instead, C++ is based on Member
    Initialization List. the derived class constructor should call the
    base class constructor (the default constructor can be invoked
    implicitly). Such initialization, initilizes the base part of derived
    class object.

    Regards,
    S. Amrollahi
     
    Saeed Amrollahi, Nov 5, 2007
    #3
  4. Rahul

    Ron Natalie Guest

    Rahul wrote:
    > Hi Everyone,
    >
    > While working with Java, i came across super() which passes values to
    > base class constructor from derived class constructor. I was wondering
    > if this could be implemented in c++ by any mechanism as super is not
    > supported by c++, atleast by MS vc++ 6.0.
    >

    C++ is not Java. Contructors are automatically called in the
    language-defined sequence in C++. You can not alter that NOR
    can you call them directly.
     
    Ron Natalie, Nov 5, 2007
    #4
  5. Rahul

    LR Guest

    Ron Natalie wrote:
    > Rahul wrote:
    >> Hi Everyone,
    >>
    >> While working with Java, i came across super() which passes values to
    >> base class constructor from derived class constructor. I was wondering
    >> if this could be implemented in c++ by any mechanism as super is not
    >> supported by c++, atleast by MS vc++ 6.0.
    >>

    > C++ is not Java. Contructors are automatically called in the
    > language-defined sequence in C++. You can not alter that NOR
    > can you call them directly.



    I'm not sure that I follow that exactly.

    What about:

    class SomeClass : public BaseClass {
    public:
    SomeClass() : BaseClass(94) {}
    };

    Not a good example, and I left out what BaseClass looks like.

    I guess that perhaps it depends on what you meant by directly.

    LR
     
    LR, Nov 5, 2007
    #5
  6. Rahul

    James Kanze Guest

    On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    > Rahul wrote:
    > > While working with Java, i came across super() which passes
    > > values to base class constructor from derived class
    > > constructor. I was wondering if this could be implemented
    > > in c++ by any mechanism as super is not supported by c++,
    > > atleast by MS vc++ 6.0.


    > C++ is not Java. Contructors are automatically called in the
    > language-defined sequence in C++. You can not alter that NOR
    > can you call them directly.


    That's true in Java as well, with the restriction that 1) you
    can only have a single base class, and 2) members are either
    basic types or pointers, and don't have constructors. The only
    difference is the syntax you use to pass arguments to the base
    class constructor. In Java, since there's only one, the keyword
    super() is sufficient, and Java "lies" a bit to you by using a
    function call syntax---although the function call must be the
    first thing you do in the constructor. C++ uses a special
    syntax, called the initialization list, associated with
    constructors (but also "lies" a little to you in that the order
    of the list is ignored).

    --
    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
     
    James Kanze, Nov 5, 2007
    #6
  7. * James Kanze:
    > On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    >> Rahul wrote:
    >>> While working with Java, i came across super() which passes
    >>> values to base class constructor from derived class
    >>> constructor. I was wondering if this could be implemented
    >>> in c++ by any mechanism as super is not supported by c++,
    >>> atleast by MS vc++ 6.0.

    >
    >> C++ is not Java. Contructors are automatically called in the
    >> language-defined sequence in C++. You can not alter that NOR
    >> can you call them directly.

    >
    > That's true in Java as well, with the restriction that 1) you
    > can only have a single base class, and 2) members are either
    > basic types or pointers, and don't have constructors. The only
    > difference is the syntax you use to pass arguments to the base
    > class constructor. In Java, since there's only one, the keyword
    > super() is sufficient,


    In most cases super() would be sufficient in C++ too.

    And Visual C++ provides that as a language extension, '__super', relying
    on ordinary overload resolution, see e.g. <url:
    http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.

    To some degree one may simulate the effect of '__super' by typedef'ing a
    'Base',

    typedef MyBaseClass Base;

    but in particular, when MyBaseClass is something like X<Y, Z, ...> it
    makes you repeat the whole shebang.

    'super' would have been a great addition to C++0x.

    Especially since there's years and years of existing practice... ;-)


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 5, 2007
    #7
  8. Alf P. Steinbach wrote:

    > * James Kanze:
    >> On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    >>> Rahul wrote:
    >>>> While working with Java, i came across super() which passes
    >>>> values to base class constructor from derived class
    >>>> constructor. I was wondering if this could be implemented
    >>>> in c++ by any mechanism as super is not supported by c++,
    >>>> atleast by MS vc++ 6.0.

    >>
    >>> C++ is not Java. Contructors are automatically called in the
    >>> language-defined sequence in C++. You can not alter that NOR
    >>> can you call them directly.

    >>
    >> That's true in Java as well, with the restriction that 1) you
    >> can only have a single base class, and 2) members are either
    >> basic types or pointers, and don't have constructors. The only
    >> difference is the syntax you use to pass arguments to the base
    >> class constructor. In Java, since there's only one, the keyword
    >> super() is sufficient,

    >
    > In most cases super() would be sufficient in C++ too.
    >
    > And Visual C++ provides that as a language extension, '__super', relying
    > on ordinary overload resolution, see e.g. <url:
    > http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.
    >
    > To some degree one may simulate the effect of '__super' by typedef'ing a
    > 'Base',
    >
    > typedef MyBaseClass Base;
    >
    > but in particular, when MyBaseClass is something like X<Y, Z, ...> it
    > makes you repeat the whole shebang.
    >
    > 'super' would have been a great addition to C++0x.
    >
    > Especially since there's years and years of existing practice... ;-)


    Hmmm...

    What's the advantage of the super-syntax over explicit initialisation lists
    resp. scope resolution with "::"? All I see is the danger of obfuscation in
    the case of multiple inheritance. I wonder if this would justify another
    keyword, especially a common one like super. But I don't do Java, so I may
    be missing something!
     
    Paul Brettschneider, Nov 5, 2007
    #8
  9. Rahul

    James Kanze Guest

    On Nov 5, 7:01 pm, "Alf P. Steinbach" <> wrote:
    > * James Kanze:
    > > On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    > >> Rahul wrote:
    > >>> While working with Java, i came across super() which passes
    > >>> values to base class constructor from derived class
    > >>> constructor. I was wondering if this could be implemented
    > >>> in c++ by any mechanism as super is not supported by c++,
    > >>> atleast by MS vc++ 6.0.


    > >> C++ is not Java. Contructors are automatically called in the
    > >> language-defined sequence in C++. You can not alter that NOR
    > >> can you call them directly.


    > > That's true in Java as well, with the restriction that 1) you
    > > can only have a single base class, and 2) members are either
    > > basic types or pointers, and don't have constructors. The only
    > > difference is the syntax you use to pass arguments to the base
    > > class constructor. In Java, since there's only one, the keyword
    > > super() is sufficient,


    > In most cases super() would be sufficient in C++ too.


    The way Java uses it? Only in the case of single inheritance.

    A reasonable extension might be to provide a keyword super, with
    semantics something like those of this, except that name lookup
    after it ignores the current class (and only considers base
    classes). Since it would define yet another context with its
    own rules for name lookup, writing it up would require a good
    deal of work; the benefits probably aren't worth it.

    > And Visual C++ provides that as a language extension,
    > '__super', relying on ordinary overload resolution, see e.g.
    > <url:http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.


    > To some degree one may simulate the effect of '__super' by
    > typedef'ing a 'Base',


    > typedef MyBaseClass Base;


    > but in particular, when MyBaseClass is something like X<Y, Z,
    > ...> it makes you repeat the whole shebang.


    And in the case of multiple inheritance, it doesn't work at all.

    > 'super' would have been a great addition to C++0x.


    > Especially since there's years and years of existing practice... ;-)


    There was actually some discussion about it at the beginning of
    the C++ standardization effort. Then someone pointed out the
    typedef solution, and it was decided that since it was so easily
    simulated, the effort wasn't worth it. Given the fact that the
    typedef solution doesn't work with multiple inheritance, and is,
    as you point out, very long winded (and fragile) when templated
    base classes are involved, the issue could be reconsidered.

    But I think it's a bit late for this round of standardization.
    I don't think support for either super::something or
    super->something would be that difficult, but there's a good
    deal of text which would have to be added section 3.4, since
    name lookup in such cases isn't exactly like any existing name
    lookup. And other parts of the standard might be affected as
    well: a priori, I would expect either form above to make the
    name dependent, for example, so there would be modifications in
    section 14 as well.

    --
    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
     
    James Kanze, Nov 6, 2007
    #9
  10. Rahul

    James Kanze Guest

    On Nov 5, 8:56 pm, Paul Brettschneider <>
    wrote:
    > Alf P. Steinbach wrote:
    > > * James Kanze:
    > >> On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    > >>> Rahul wrote:
    > >>>> While working with Java, i came across super() which passes
    > >>>> values to base class constructor from derived class
    > >>>> constructor. I was wondering if this could be implemented
    > >>>> in c++ by any mechanism as super is not supported by c++,
    > >>>> atleast by MS vc++ 6.0.


    > >>> C++ is not Java. Contructors are automatically called in the
    > >>> language-defined sequence in C++. You can not alter that NOR
    > >>> can you call them directly.


    > >> That's true in Java as well, with the restriction that 1) you
    > >> can only have a single base class, and 2) members are either
    > >> basic types or pointers, and don't have constructors. The only
    > >> difference is the syntax you use to pass arguments to the base
    > >> class constructor. In Java, since there's only one, the keyword
    > >> super() is sufficient,


    > > In most cases super() would be sufficient in C++ too.


    > > And Visual C++ provides that as a language extension, '__super', relying
    > > on ordinary overload resolution, see e.g. <url:
    > >http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.


    > > To some degree one may simulate the effect of '__super' by typedef'ing a
    > > 'Base',


    > > typedef MyBaseClass Base;


    > > but in particular, when MyBaseClass is something like X<Y, Z, ...> it
    > > makes you repeat the whole shebang.


    > > 'super' would have been a great addition to C++0x.


    > > Especially since there's years and years of existing practice... ;-)


    > What's the advantage of the super-syntax over explicit
    > initialisation lists resp. scope resolution with "::"? All I
    > see is the danger of obfuscation in the case of multiple
    > inheritance. I wonder if this would justify another keyword,
    > especially a common one like super. But I don't do Java, so I
    > may be missing something!


    Well, IIUC, what Alf is proposing is quite unlike the super in
    Java (which corresponds to the C++ initialization list). What
    he is proposing would be useful, in at least two cases. The is
    precisely multiple inheritance, where you don't care which base
    class provided the function. The other would be in the case Alf
    mentions, where the base class "name" is actually a very complex
    template instantiation. Not having to repeat it is a definite
    advantage. More generally, using super probably enhances
    readability: the important thing when you use it isn't that you
    are using a class named Base, it's that you're using the class
    from which this class (or one of the classes) derives. It
    expresses intent more clearly.

    --
    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
     
    James Kanze, Nov 6, 2007
    #10
  11. Rahul

    Ron Natalie Guest

    LR wrote:

    >>>

    >> C++ is not Java. Contructors are automatically called in the
    >> language-defined sequence in C++. You can not alter that NOR
    >> can you call them directly.

    >
    >
    > I'm not sure that I follow that exactly.
    >
    > What about:
    >
    > class SomeClass : public BaseClass {
    > public:
    > SomeClass() : BaseClass(94) {}
    > };
    >
    > Not a good example, and I left out what BaseClass looks like.
    >

    All that does is say that when BaseClass is initialized, it is
    initialized with 94. The mem-init list there is not a list of
    constructor calls, the ordering of the inits (or their absence)
    does not alter the invocation of the constructors, and in fact
    doesn't indicate that there is a constructor there at all (non
    -class member
    initializers can also be specified there).
     
    Ron Natalie, Nov 6, 2007
    #11
  12. Rahul

    Ron Natalie Guest

    Alf P. Steinbach wrote:

    >
    > In most cases super() would be sufficient in C++ too.
    >
    > And Visual C++ provides that as a language extension, '__super', relying
    > on ordinary overload resolution, see e.g. <url:
    > http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.
    >
    > To some degree one may simulate the effect of '__super' by typedef'ing a
    > 'Base',
    >
    > typedef MyBaseClass Base;
    >


    The previous attempt at a dynamically downloadable object oriented
    language that Mr. Gosling and compatriots worked on before Java,
    had an interesting way of handling super class chaining. The
    super class invocation just removed the current classes definition
    of the method, as a result the method would resolve into whatever
    base class defined it in the case of multiple inheritance.

    Of course, how you'd write that in a C++ syntax I have no idea.
     
    Ron Natalie, Nov 6, 2007
    #12
  13. Rahul

    terminator Guest

    One can(had better) use initializer lists for this purpose or default
    constructors are called for direct base classes and data members and
    just then comes the turn to execute the statements in the definition
    block of the class`s constructor.

    On Nov 5, 3:47 pm, Ron Natalie <> wrote:
    > Rahul wrote:
    > > Hi Everyone,

    >
    > > While working with Java, i came across super() which passes values to
    > > base class constructor from derived class constructor. I was wondering
    > > if this could be implemented in c++ by any mechanism as super is not
    > > supported by c++, atleast by MS vc++ 6.0.

    >
    > C++ is not Java. Contructors are automatically called in the
    > language-defined sequence in C++. You can not alter that NOR
    > can you call them directly.


    seems you hav not read Mike`s post yet:

    On Nov 5, 4:48 am, "Mike Wahler" <> wrote:
    > "Rahul" <> wrote in message
    >
    > news:...
    >
    > #include <iostream>
    >
    > class Base
    > {
    > int i;
    > public:
    > Base(int arg_b = 0) : i(arg_b)
    > {
    > std::cout << "Base constructor: i = " << i << '\n';
    > }
    >
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(int arg_d) : Base(arg_d)
    > {

    //terminator includes:
    std::cout << "Derived constructor.\n";
    > }
    >
    > };
    >
    > int main()
    > {
    > Derived d(42);
    > return 0;
    >
    > }
    >
    > Output:
    > Base constructor: i = 42

    Derived costructor.
    >
    > -Mike


    however initializer lists can only be used for direct base classes(no
    grand papas allowed) and direct(not inherited) data members.

    regards,
    FM
     
    terminator, Nov 6, 2007
    #13
  14. Rahul

    terminator Guest

    On Nov 6, 12:03 pm, James Kanze <> wrote:
    > On Nov 5, 8:56 pm, Paul Brettschneider <>
    > wrote:
    >
    >
    >
    >
    >
    > > Alf P. Steinbach wrote:
    > > > * James Kanze:
    > > >> On Nov 5, 1:47 pm, Ron Natalie <> wrote:
    > > >>> Rahul wrote:
    > > >>>> While working with Java, i came across super() which passes
    > > >>>> values to base class constructor from derived class
    > > >>>> constructor. I was wondering if this could be implemented
    > > >>>> in c++ by any mechanism as super is not supported by c++,
    > > >>>> atleast by MS vc++ 6.0.
    > > >>> C++ is not Java. Contructors are automatically called in the
    > > >>> language-defined sequence in C++. You can not alter that NOR
    > > >>> can you call them directly.
    > > >> That's true in Java as well, with the restriction that 1) you
    > > >> can only have a single base class, and 2) members are either
    > > >> basic types or pointers, and don't have constructors. The only
    > > >> difference is the syntax you use to pass arguments to the base
    > > >> class constructor. In Java, since there's only one, the keyword
    > > >> super() is sufficient,
    > > > In most cases super() would be sufficient in C++ too.
    > > > And Visual C++ provides that as a language extension, '__super', relying
    > > > on ordinary overload resolution, see e.g. <url:
    > > >http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.
    > > > To some degree one may simulate the effect of '__super' by typedef'ing a
    > > > 'Base',
    > > > typedef MyBaseClass Base;
    > > > but in particular, when MyBaseClass is something like X<Y, Z, ...> it
    > > > makes you repeat the whole shebang.
    > > > 'super' would have been a great addition to C++0x.
    > > > Especially since there's years and years of existing practice... ;-)

    > > What's the advantage of the super-syntax over explicit
    > > initialisation lists resp. scope resolution with "::"? All I
    > > see is the danger of obfuscation in the case of multiple
    > > inheritance. I wonder if this would justify another keyword,
    > > especially a common one like super. But I don't do Java, so I
    > > may be missing something!

    >
    > Well, IIUC, what Alf is proposing is quite unlike the super in
    > Java (which corresponds to the C++ initialization list). What
    > he is proposing would be useful, in at least two cases. The is
    > precisely multiple inheritance, where you don't care which base
    > class provided the function. The other would be in the case Alf
    > mentions, where the base class "name" is actually a very complex
    > template instantiation. Not having to repeat it is a definite
    > advantage. More generally, using super probably enhances
    > readability: the important thing when you use it isn't that you
    > are using a class named Base, it's that you're using the class
    > from which this class (or one of the classes) derives. It
    > expresses intent more clearly.
    >
    > --
    > 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- Hide quoted text -
    >
    > - Show quoted text -


    'super' can become a compile-time array of types in case of using
    multiple inheritance:

    struct multiple:A,B,C,D{
    enum{first=0};
    multiple(params):
    super<0>(params),//new syntax: super<internal_integral_expr>
    super<1>(params),
    super<2>(params),
    super<3>(params)
    {
    super<first>::foo();
    ...
    }
    };

    struct single: A{
    multiple(params):super(params){
    super::foo();
    };
    };

    regards,
    FM.
     
    terminator, Nov 6, 2007
    #14
  15. Ron Natalie wrote:

    > Alf P. Steinbach wrote:
    >
    >>
    >> In most cases super() would be sufficient in C++ too.
    >>
    >> And Visual C++ provides that as a language extension, '__super', relying
    >> on ordinary overload resolution, see e.g. <url:
    >> http://msdn2.microsoft.com/en-us/library/94dw1w7x(VS.80).aspx>.
    >>
    >> To some degree one may simulate the effect of '__super' by typedef'ing a
    >> 'Base',
    >>
    >> typedef MyBaseClass Base;
    >>

    >
    > The previous attempt at a dynamically downloadable object oriented
    > language that Mr. Gosling and compatriots worked on before Java,
    > had an interesting way of handling super class chaining. The
    > super class invocation just removed the current classes definition
    > of the method, as a result the method would resolve into whatever
    > base class defined it in the case of multiple inheritance.


    That makes some sense.

    > Of course, how you'd write that in a C++ syntax I have no idea.


    Being a UNIX user I suggest "..::". Of course with multiple inheritance
    things like "..::..::" are funny. I guess it should remove the definitions
    of all parent-classes as well?
     
    Paul Brettschneider, Nov 7, 2007
    #15
  16. Rahul

    Ron Natalie Guest

    terminator wrote:
    > One can(had better) use initializer lists for this purpose or default
    > constructors are called for direct base classes and data members and
    > just then comes the turn to execute the statements in the definition
    > block of the class`s constructor.


    Absolutely, the order of initialization is this:

    1. For only the most derived class walk the inheritance tree
    initializing the virtual base classes (with parameters
    if any from the most derived class constructor initializer list).

    Then recursively working from the most derived object:

    2. Initialize the direct non-virtual base classes (using
    the mem inits from the current class construct initialier list).

    3. Initialize the non-static direct members (again using
    the mem initializer list)

    4. Invoke the constructor body.
     
    Ron Natalie, Nov 7, 2007
    #16
  17. Rahul

    terminator Guest

    On Nov 7, 3:10 pm, Ron Natalie <> wrote:

    > 1. For only the most derived class walk the inheritance tree
    > initializing the virtual base classes (with parameters
    > if any from the most derived class constructor initializer list).
    >


    Good point, that is the question I have always forgotten to ask:
    How is the con/de-struction order of virtual base classes?
    What should the initializer lists of virtually derived classes look
    like?
    The same virtual subobject should not be constructed twice ,So I have
    got a serious question about semantics for this case.

    regards,
    FM.
     
    terminator, Nov 7, 2007
    #17
    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. mrstephengross
    Replies:
    5
    Views:
    598
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. Replies:
    2
    Views:
    715
Loading...

Share This Page