473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

call base class constructor from derived class constructor

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
11 18305

"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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
5
by: hillcountry74 | last post by:
Hi, I'm a newbie to OOP and VB.Net. In the business layer, I have a base class, which is an abstract class in VB.Net. The derived class overrides a method and calls other methods in the base...
1
by: question | last post by:
I want to know incase there is any performance difference or overhead in calling a base class method and a derived class method. Basically I am talking about simple method that is not overridden...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
11
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
I have a simple class: public class BaseClass { private string propertyA; protected string PropertyA { get { return propertyA; } set { propertyA = value; }
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: brboLikus | last post by:
Hello everybody! My problem is somewhat strange since I can't think of any normal situation where one would like to do what I need, but here it goes. I need to somehow cast a base class to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.