By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

super constructor in C++

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:m2********************************@4ax.com...
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
Jul 22 '05 #2

P: n/a
"cppaddict" <he***@hello.com> wrote in message
news:m2********************************@4ax.com...
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
Jul 22 '05 #3

P: n/a
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

Jul 22 '05 #4

P: n/a
cppaddict <he***@hello.com> 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).
Jul 22 '05 #5

P: n/a
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


Jul 22 '05 #6

P: n/a
cppaddict <he***@hello.com> 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...
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.