Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > super constructor in C++

Reply
Thread Tools

super constructor in C++

 
 
cppaddict
Guest
Posts: n/a
 
      05-24-2004
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:erived(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

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      05-24-2004
"cppaddict" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Derived:erived(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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-24-2004

"cppaddict" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:erived(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


 
Reply With Quote
 
cppaddict
Guest
Posts: n/a
 
      05-24-2004

>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

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-24-2004
cppaddict <(E-Mail Removed)> 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:erived(int x) : Base(x) { }
>


In case you were unsure, if you had:
Base::Base(void) { }
then
Derived:erived(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).
 
Reply With Quote
 
himanshu bafna
Guest
Posts: n/a
 
      05-25-2004
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:erived(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
>


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      05-26-2004
cppaddict <(E-Mail Removed)> 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:erived(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...
 
Reply With Quote
 
mherstand mherstand is offline
Junior Member
Join Date: Apr 2009
Posts: 1
 
      04-23-2009
Quote:
Originally Posted by Daniel T.
cppaddict <(E-Mail Removed)> wrote:
[color=blue]
//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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
super.super.super how? Java 24 02-24-2005 10:51 PM
super. could there be a simpler super? Kerim Borchaev Python 4 01-15-2004 03:15 PM
WHICH LENS? - SIGMA 70-300mm DL MACRO Super-II or 28-300mm AF Super Zoom 4 - for Canon 300d Rebel Digital SLR? No Spam Digital Photography 24 11-27-2003 08:21 AM
Getting the super class via the super() function Fernando Rodriguez Python 2 11-22-2003 12:08 AM



Advertisments