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

call base class constructor from derived class constructor

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

Nov 5 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Rahul" <sa*****@yahoo.co.inwrote in message
news:11**********************@v29g2000prd.googlegr oups.com...
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

Nov 5 '07 #2

P: n/a
On Nov 5, 3:12 am, Rahul <sam_...@yahoo.co.inwrote:
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

Nov 5 '07 #3

P: n/a
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.
Nov 5 '07 #4

P: n/a
LR
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
Nov 5 '07 #5

P: n/a
Alf P. Steinbach wrote:
* James Kanze:
>On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.netwrote:
>>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!
Nov 5 '07 #6

P: n/a
On Nov 5, 7:01 pm, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.netwrote:
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:ja*********@gmail.com
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

Nov 6 '07 #7

P: n/a
On Nov 5, 8:56 pm, Paul Brettschneider <paul.brettschnei...@yahoo.fr>
wrote:
Alf P. Steinbach wrote:
* James Kanze:
On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.netwrote:
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:ja*********@gmail.com
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

Nov 6 '07 #8

P: n/a
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.
Nov 6 '07 #9

P: n/a
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 <r...@spamcop.netwrote:
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" <mkwah...@mkwahler.netwrote:
"Rahul" <sam_...@yahoo.co.inwrote in message

news:11**********************@v29g2000prd.googlegr oups.com...

#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
Nov 6 '07 #10

P: n/a
On Nov 6, 12:03 pm, James Kanze <james.ka...@gmail.comwrote:
On Nov 5, 8:56 pm, Paul Brettschneider <paul.brettschnei...@yahoo.fr>
wrote:


Alf P. Steinbach wrote:
* James Kanze:
>On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.netwrote:
>>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:james.ka...@gmail.com
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.

Nov 6 '07 #11

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

Nov 7 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.