473,698 Members | 2,246 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
11 18336
On Nov 6, 12:03 pm, James Kanze <james.ka...@gm ail.comwrote:
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:james.ka. ..@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- 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>::f oo();
...
}
};

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
2523
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
3
1913
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
3264
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
1560
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 invoke these: derved.M1() derived.M2()
4
5242
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 thoughts on the practice below? class Base { public:
8
2017
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: "foo" |------------method: "rob_the_liquor_store()" |
11
3271
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
1972
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 `MakeBase()` function. It all makes sense to me, but I don't seem to find an appropriate solution. Since I...
0
1447
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
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5862
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.