super constructor in C++

Discussion in 'C++' started by cppaddict, May 24, 2004.

  1. cppaddict

    cppaddict Guest

    Hi,

    I know that C++ does not have an explicit super() constructor for
    calling a Base class constructor from a Derived class's constructor,
    but my understanding is that C++ implements this functionality via its
    initialization list.

    Say I have a Base class with a constructor that looks like:

    Base::Base(int x) {
    //does stuff #1
    }

    Now if I want my Derived class also to have a constructor that takes
    an int, does the same stuff as Base(int), and then does some
    additional stuff, can I simply do:

    Derived::Derived(int x) : Base(x) { //that call should do stuff #1
    //does stuff #2
    }

    The above code (when filled in properly) compiles and runs, but I want
    to make sure my understanding is correct. Is the initialization list,
    as used above, essentially the C++ syntactic analog of Java's super()?
    If not, what are the differences?

    Thanks for any clarification,
    cpp
     
    cppaddict, May 24, 2004
    #1
    1. Advertising

  2. "cppaddict" <> wrote in message
    news:...
    > Derived::Derived(int x) : Base(x) { //that call should do stuff #1
    > //does stuff #2
    > }
    >
    > The above code (when filled in properly) compiles and runs, but I want
    > to make sure my understanding is correct. Is the initialization list,
    > as used above, essentially the C++ syntactic analog of Java's super()?

    Yes.

    > If not, what are the differences?

    There are subtle behavioral differences. In particular, the Derived
    object does not 'exist' yet when the base class constructor is
    called. This implies, for instance, that the base constructor
    cannot call a virtual function implemented in Derived.

    Java's constructors behave more like a virtual init() function
    that would be called after the C++ constructors are executed.

    Cheers,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
     
    Ivan Vecerina, May 24, 2004
    #2
    1. Advertising

  3. "cppaddict" <> wrote in message
    news:...
    > Hi,
    >
    > I know that C++ does not have an explicit super() constructor for
    > calling a Base class constructor from a Derived class's constructor,
    > but my understanding is that C++ implements this functionality via its
    > initialization list.
    >
    > Say I have a Base class with a constructor that looks like:
    >
    > Base::Base(int x) {
    > //does stuff #1
    > }
    >
    > Now if I want my Derived class also to have a constructor that takes
    > an int, does the same stuff as Base(int), and then does some
    > additional stuff, can I simply do:
    >
    > Derived::Derived(int x) : Base(x) { //that call should do stuff #1
    > //does stuff #2
    > }
    >
    > The above code (when filled in properly) compiles and runs, but I want
    > to make sure my understanding is correct.


    Your understanding is correct.

    > Is the initialization list,
    > as used above, essentially the C++ syntactic analog of Java's super()?


    Not completely familar with Java, but yes I would say so.

    > If not, what are the differences?
    >
    > Thanks for any clarification,
    > cpp
    >


    john
     
    John Harrison, May 24, 2004
    #3
  4. cppaddict

    cppaddict Guest


    >Your understanding is correct.
    >
    >> Is the initialization list,
    >> as used above, essentially the C++ syntactic analog of Java's super()?

    >
    >Not completely familar with Java, but yes I would say so.


    John,

    Thanks for the verification.

    cpp
     
    cppaddict, May 24, 2004
    #4
  5. cppaddict

    Old Wolf Guest

    cppaddict <> wrote:
    > Hi,
    >
    > I know that C++ does not have an explicit super() constructor for
    > calling a Base class constructor from a Derived class's constructor,
    > but my understanding is that C++ implements this functionality via its
    > initialization list.
    >
    > Base::Base(int x) { }
    >
    > Now if I want my Derived class also to have a constructor that takes
    > an int, does the same stuff as Base(int), and then does some
    > additional stuff, can I simply do:
    >
    > Derived::Derived(int x) : Base(x) { }
    >


    In case you were unsure, if you had:
    Base::Base(void) { }
    then
    Derived::Derived(int x) { }
    calls Base::Base() too (ie. if you don't include a base class in the
    initializer list, then the base class's default constructor is called).

    In C++, the base constructors all run before the derived constructor
    is called. Also, the order they run in is determined by the class
    hierarchy and the other they are declared (not the order they appear
    in the initializer list).

    This is different to some other languages (and some versions of C++
    when not operating in Standard mode). I can't speak for Java,
    but in Object Pascal, memory is allocated, and then only the
    most-derived class constructor is called (and it's responsible
    for calling parent constructors at the appropriate time).
     
    Old Wolf, May 24, 2004
    #5
  6. Hi

    the code snippet is correct in the sense that you can call the base
    class's constructor and pass arguments to it without eplicitly calling
    it. This is done when we create a object of the derived class. In this
    first the base object is constructed over which the derived class object
    is constructed. therefore the base constructor gets called first and
    if it takes in an argument then this is the correct way of doing it.

    Dont know about Java's thing.

    Regards
    Himanshu



    cppaddict wrote:
    > Hi,
    >
    > I know that C++ does not have an explicit super() constructor for
    > calling a Base class constructor from a Derived class's constructor,
    > but my understanding is that C++ implements this functionality via its
    > initialization list.
    >
    > Say I have a Base class with a constructor that looks like:
    >
    > Base::Base(int x) {
    > //does stuff #1
    > }
    >
    > Now if I want my Derived class also to have a constructor that takes
    > an int, does the same stuff as Base(int), and then does some
    > additional stuff, can I simply do:
    >
    > Derived::Derived(int x) : Base(x) { //that call should do stuff #1
    > //does stuff #2
    > }
    >
    > The above code (when filled in properly) compiles and runs, but I want
    > to make sure my understanding is correct. Is the initialization list,
    > as used above, essentially the C++ syntactic analog of Java's super()?
    > If not, what are the differences?
    >
    > Thanks for any clarification,
    > cpp
    >
     
    himanshu bafna, May 25, 2004
    #6
  7. cppaddict

    Daniel T. Guest

    cppaddict <> wrote:

    >Hi,
    >
    >I know that C++ does not have an explicit super() constructor for
    >calling a Base class constructor from a Derived class's constructor,
    >but my understanding is that C++ implements this functionality via its
    >initialization list.
    >
    >Say I have a Base class with a constructor that looks like:
    >
    >Base::Base(int x) {
    > //does stuff #1
    >}
    >
    >Now if I want my Derived class also to have a constructor that takes
    >an int, does the same stuff as Base(int), and then does some
    >additional stuff, can I simply do:
    >
    >Derived::Derived(int x) : Base(x) { //that call should do stuff #1
    > //does stuff #2
    >}
    >
    >The above code (when filled in properly) compiles and runs, but I want
    >to make sure my understanding is correct. Is the initialization list,
    >as used above, essentially the C++ syntactic analog of Java's super()?


    No.

    >If not, what are the differences?


    //C++

    class Base {
    virtual void foo() { cout << "base\n"; }
    protected:
    Base() { foo(); }
    };

    class Derived: public Base {
    virtual void foo() { cout << "derived\n"; }
    public:
    Derived() { foo(); }
    };

    int main() {
    Derived b;
    }

    Produces the output:
    base
    derived

    //Java

    class Base {
    protected void foo() { System.out.println( "Base" ); }
    public Base() { foo(); }
    }

    public class Derived extends Base {
    protected void foo() { System.out.println( "derived" ); }
    public Derived() { foo(); }

    public static void main( String args[] ) {
    new Derived();
    }
    }

    Produces the output:
    derived
    derived


    From this we can see that the two languages have very different ideas
    about when the object comes into existance and what code is called
    before the object is fully formed and what code is called after. In
    Java, the object is a Derived before any method is executed on it, in
    C++ the object is not a Derived until after the initialization list is
    complete.

    Not that you asked but Python does it different still:

    #Python
    class Base:
    def foo(self):
    print "base"

    def __init__(self):
    self.foo()

    class Derived (Base):
    def foo(self):
    print "derived"

    def __init__(self):
    self.foo()

    Derived()

    Produces the output:
    derived

    You see in Python, the base class init method isn't automatically
    called...
     
    Daniel T., May 26, 2004
    #7
  8. cppaddict

    mherstand

    Joined:
    Apr 23, 2009
    Messages:
    1


    I know this is old, but I'm pretty sure that's wrong. Wouldn't it only output "derived" once? Unless you call super, the super constructor is never called in Java. (I'm not positive about this).
     
    mherstand, Apr 23, 2009
    #8
    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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,800
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    724
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    479
    Michele Simionato
    Jan 15, 2004
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. GOOD LUCK GROUP
    Replies:
    0
    Views:
    402
    GOOD LUCK GROUP
    Apr 28, 2008
Loading...

Share This Page