super constructor in C++

C

cppaddict

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
 
I

Ivan Vecerina

cppaddict said:
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
 
J

John Harrison

cppaddict said:
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
 
O

Old Wolf

cppaddict said:
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).
 
H

himanshu bafna

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
 
D

Daniel T.

cppaddict said:
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...
 
Joined
Apr 23, 2009
Messages
1
Reaction score
0
Daniel T. said:
cppaddict <[email protected]> wrote:

//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




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

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top