473,385 Members | 1,720 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,385 software developers and data experts.

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 1856
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
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
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...
7
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...
4
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
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
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...
5
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
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...
11
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
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,...
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
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,...
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.