Calling base class constructor?

F

Fedor Semenov

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?
}
};
 
J

Jerry Coffin

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?
}
};

This is probably in the FAQ somewhere. Given that your
base class ctor doesn't take any parameters, it'll be
called entirely automatically in the process of creating
an object.

If your base class ctor takes parameters, you can do
something like this to pass the correct parameters:

struct base {
base(int y);
};

struct derived : public base {
derived() : base(1) {}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top