call base class constructor from derived class constructor

R

Rahul

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

Mike Wahler

Rahul said:
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
 
S

Saeed Amrollahi

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
 
R

Ron Natalie

Rahul said:
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.
 
L

LR

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

James Kanze

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, and Java "lies" a bit to you by using a
function call syntax---although the function call must be the
first thing you do in the constructor. C++ uses a special
syntax, called the initialization list, associated with
constructors (but also "lies" a little to you in that the order
of the list is ignored).
 
A

Alf P. Steinbach

* James Kanze:
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... ;-)


Cheers, & hth.,

- Alf
 
P

Paul Brettschneider

Alf said:
* James Kanze:

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!
 
J

James Kanze

* James Kanze:
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.
 
J

James Kanze

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

Ron Natalie

LR said:
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.
All that does is say that when BaseClass is initialized, it is
initialized with 94. The mem-init list there is not a list of
constructor calls, the ordering of the inits (or their absence)
does not alter the invocation of the constructors, and in fact
doesn't indicate that there is a constructor there at all (non
-class member
initializers can also be specified there).
 
R

Ron Natalie

Alf said:
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.
 
T

terminator

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.

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:

#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
 
T

terminator

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:[email protected]
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.
 
P

Paul Brettschneider

Ron said:
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?
 
R

Ron Natalie

terminator said:
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.

Absolutely, the order of initialization is this:

1. For only the most derived class walk the inheritance tree
initializing the virtual base classes (with parameters
if any from the most derived class constructor initializer list).

Then recursively working from the most derived object:

2. Initialize the direct non-virtual base classes (using
the mem inits from the current class construct initialier list).

3. Initialize the non-static direct members (again using
the mem initializer list)

4. Invoke the constructor body.
 
T

terminator

1. For only the most derived class walk the inheritance tree
initializing the virtual base classes (with parameters
if any from the most derived class constructor initializer list).

Good point, that is the question I have always forgotten to ask:
How is the con/de-struction order of virtual base classes?
What should the initializer lists of virtually derived classes look
like?
The same virtual subobject should not be constructed twice ,So I have
got a serious question about semantics for this case.

regards,
FM.
 

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
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top