By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Calling base class constructor?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.