473,774 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

call constructor from base class

Hi ikind of remember this being easy but cant find it on google:
i have a base class and a derived class.
The base class has two constructors normal and int overloaded one.
thing is i want the derived class to call the constructors for the base
class using also the same kind of overloading. like this:

class BASE{
public:
BASE();
BASE(int x);
}

class DERIVED : BASE{
//normal constructor
DERIVED(){
//init BASE with normal constructor
};
//overloaded constructor
DERIVED(int){
//init BASE with overloaded constructor
};
}

int main(){

//this should call the overloaded constructor of BASE!!!
DERIVED dummy(3);
return 1;
}

it would be my death if the wrong base constructor would be called or
even called twice...
thanks for any help!

May 12 '06 #1
1 1879
mw****@freenet. de wrote:
Hi ikind of remember this being easy but cant find it on google:
i have a base class and a derived class.
The base class has two constructors normal and int overloaded one.
thing is i want the derived class to call the constructors for the base
class using also the same kind of overloading. like this:
Just put the proper base constructor call in your derived class'
initialization list. See below.

class BASE{
Don't use all caps for your names. Reserve that for macros only (and
use those only sparingly!).
public:
BASE();
BASE(int x);
}

class DERIVED : BASE{
//normal constructor
DERIVED() : BASE() // Would be called by default anyway {
//init BASE with normal constructor
};
//overloaded constructor
DERIVED(int i) : BASE( i ) {
//init BASE with overloaded constructor
};
}

int main(){

//this should call the overloaded constructor of BASE!!!
DERIVED dummy(3);
It will!!!
return 1;
}

it would be my death if the wrong base constructor would be called or
even called twice...
thanks for any help!


Cheers! --M

May 12 '06 #2

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

Similar topics

23
5184
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
2981
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
7
9972
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
4
2068
by: Greg | last post by:
Is it possible to call a constructor from within a constructor I mean Class A { public A(string getModifiedVal) { .........
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
6
4339
by: marcwentink | last post by:
Say I have class A that inherits from class B, and I call class A his constructor. Then somehow class B his constructor is called also. How does this work? Is a constructor under the hood a sort of virtual method by default, and are the ancestors constructors called in a sort of VT table?
5
3348
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
11
18347
by: Rahul | last post by:
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.
11
2868
by: dolphin | last post by:
Hi All! I have a question that how to call a function just using a string. For example There is a .cpp file named a.cpp.There are some functions::fun1() fun2() fun3(). I have another fucntion void funcall( char *pch). if I pass a argument char* p1="fun1" .How do I call the function fun1() using that string "fun1"that I pass.
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
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
10106
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
10039
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
9914
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
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...
1
4012
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
3610
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.