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

Calling base class constructor?

How can I call a base class' constructor from a derived class. Suppose I
have:
class Button // Base class
{
public:
Button(void)
{
// Register classes, create the button, etc.
}
};

class MyButton : public Button // Derived class
{
public:
MyButton(void)
{
// Customize title, size, etc.
// How do I call Button's (base class) constructor here?
}
};

Apr 22 '06 #1
4 3915
Fedor Semenov wrote:
How can I call a base class' constructor from a derived class.
You cannot call a constructor, ever, from anywhere.
Suppose I have:
class Button // Base class
{
public:
Button(void)
{
// Register classes, create the button, etc.
}
};

class MyButton : public Button // Derived class
{
public:
MyButton(void)
{
// Customize title, size, etc.
// How do I call Button's (base class) constructor here?
You don't. What you can do is _initialise_ the base class subobject.
Read about "constructor initialiser list". Read the FAQ, while you're
at it.
}
};


V
--
Please remove capital As from my address when replying by mail
Apr 22 '06 #2
Actaully, there's a sligh workaround, but I'm not sure of how much
worth it really is.
To do this, you use the "new" operator, but you supply the memory
address of the object you wish to call the constructor upon. For
example:

class Base {
public:
Base() { cout << "Construct'd\n"; }
~Base() { cout << "Destruct'd\n"; }
};

int main() {
Base b;
Base *pb=new Base();
Base *pb2=(Base*)malloc(sizeof(Base));
new (pb2) Base; //Right here, the constructor is called
pb2->~Base();
free(pb2);
delete pb;
getch();
return 0;
}

Ouput before "getch()":
Construct'd
Construct'd
Construct'd
Destruct'd
Destruct'd

Apr 22 '06 #3
Fedor Semenov wrote:
How can I call a base class' constructor from a derived class. Suppose I
have:
class Button // Base class
{
public:
Button(void)
{
// Register classes, create the button, etc.
}
};

class MyButton : public Button // Derived class
{
public:
MyButton(void)
{
// Customize title, size, etc.
// How do I call Button's (base class) constructor here?
}
};


Its impossible to create an object of type MyButton above if MyButton's
ctor is unable to invoke a base class' ctor.

You can't call a ctor. Or rather: you definitely wouldn't want to even
if you could. invoking means 'accessing something that is already
available but not over-rideable'. A 'call' implies something you can
overide.

I'm not allowed to redefine the Button() ctor within the MyButton class.
That can only be done from within the Button class. Thats why call !=
invocation.

Hence, the above MyButton class is actually:

class MyButton : public Button
{
public:
MyButton() : Button() // the defaults
{
}
};

Test it. Change the base class to:

class Button
{
int width;
public:
Button(int n) : width(n) { }
};

and the MyButton class can no longer generate an instance.
.... unless ...

class MyButton : public Button
{
public:
MyButton() : Button(100)
{
}
};
___
Alternatively, here is a MyButton object with a preset width of 100:

class Button
{
int width;
public:
Button() : width(100) { } // a default ctor !!!
};

class MyButton : public Button
{
public:
MyButton() // no problem, a default Button() is available
{
}
};

int main()
{
MyButton button; // width = 100, the default
}

and nothing is stopping you from providing additional ctors to invoke.

class Button
{
int width;
public:
Button() : width(100) { }
Button(int n) : width(n) { }
};

class MyButton : public Button
{
public:
MyButton() { }
MyButton(int n) : Button(n) { }
};

int main()
{
MyButton button; // width = 100, the default
MyButton anotherbutton(200); // width = 200
MyButton yetanotherbutton(480); // width = 480
}


Apr 23 '06 #4
de*****@gmail.com wrote:
Actaully, there's a sligh workaround, but I'm not sure of how much
worth it really is.
To do this, you use the "new" operator, but you supply the memory
address of the object you wish to call the constructor upon. For
example:

class Base {
public:
Base() { cout << "Construct'd\n"; }
~Base() { cout << "Destruct'd\n"; }
};

int main() {
Base b;
Base *pb=new Base();
Base *pb2=(Base*)malloc(sizeof(Base));
new (pb2) Base; //Right here, the constructor is called
pb2->~Base();
free(pb2);
delete pb;
getch();
return 0;
}

Ouput before "getch()":
Construct'd
Construct'd
Construct'd
Destruct'd
Destruct'd

Eh eh eh. Sometimes it is better to withhold certain pieces of
information from certain people. Placement new is such information and
the poster is such person.

--
VH
Apr 23 '06 #5

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

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
3
by: pantalaimon | last post by:
I'm trying to write a GUI for a game I'm making. Till now I've always done this: ChildClass(int x,int y) : ParentClass(x,y) whenever my compiler complains about "no default constructor found". But...
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
8
by: Greg Bacchus | last post by:
I have a base class with a method that is to be called in the constructor of the inheritting classes. Is there any way of determining, say, the Type of the class that is calling it. e.g. ...
7
by: Jo Vermeulen | last post by:
Hello, I was wondering how I could call the base class constructor from a derived class constructor. In Java I could do something like super(parameter); I tried base(parameter);
2
by: Josef Meile | last post by:
Hi, I have this constructor: public CExcelDatabase(string host, string user, string password, string database, bool promptCredentials, int findExcelInstance, bool readOnly) { //Some code...
2
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern...
12
by: st_ev_fe | last post by:
I've noticed that when constructing a subclass, the base class get's it's contructors called. Is there some way to avoid this? The base class has one variable, which gets initialised to 0. ...
7
by: =?ISO-8859-1?Q?Fernando_G=F3mez?= | last post by:
Hello all. I have this class with a virtual method and a constructor that calls this virtual method. A derived class overrides this virtual method, so I expected that when the base's constructor is...
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?
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...
0
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...
0
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...
0
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,...

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.