473,569 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18320

"Rahul" <sa*****@yahoo. co.inwrote in message
news:11******** **************@ v29g2000prd.goo glegroups.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.n etwrote:
>>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.no wrote:
* James Kanze:
On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.n etwrote:
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::somethin g 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 objektorientier ter Datenverarbeitu ng
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.brettschn ei...@yahoo.fr>
wrote:
Alf P. Steinbach wrote:
* James Kanze:
On Nov 5, 1:47 pm, Ron Natalie <r...@spamcop.n etwrote:
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 objektorientier ter Datenverarbeitu ng
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.n etwrote:
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...@mkwah ler.netwrote:
"Rahul" <sam_...@yahoo. co.inwrote in message

news:11******** **************@ v29g2000prd.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2513
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
3
1894
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
3257
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 class. To make it more clear: Class NotInheritable BaseAbstract Sub Method1() code
1
1551
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 nor virtual. If I declare a method in the base class say M1() and another in derived class M2(). Then I make a derived class object derived. I then...
4
5236
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 attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any...
8
2007
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 of its own, maybe specifying the liquor store over on 44th Street and 5th Avenue or something. Anyway this is what we have so far: base class:...
11
3260
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
1968
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 the thing is, I want to extend the functionality of `Base` class. I do that by inheriting `Base` class by `Derived`, but then I can't use...
0
1440
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 derived class, but from the base-class code. I would need to do something like this: class Base { public Base() { } public void Convert() ...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.