Re: delete [] of Derived objects through Base object which has virtual
by xavier <xavier>
May 13, 2008 at 12:04 AM
Barry wrote:
> subramanian100in@[EMAIL PROTECTED]
India wrote:
>> * Kai-Uwe Bux <jkherci...@[EMAIL PROTECTED]
> wrote:
>>>> subramanian10...@[EMAIL PROTECTED]
wrote:
>>
>>> Well formed -- maybe. But the code has undefined behavior as per
>>> [5.3.5/3]:
>>>
>>> ... In the second alternative (delete array) if the dynamic type of
>>> the
>>> object to be deleted differs from its static type, the behavior is
>>> undefined.
>>
>> I do not know about static and dynamic type.
>> Please give me an example so that I can understand them.
>>
>
> class Base
> {
> public:
> virtual ~Base() {}
> };
>
> class Derived : public Base
> {};
>
> int main()
> {
> Base* p = new Derived(); // static type of p is "pointer to Base"
> // dynamic type of p is "pointer to Derived"
> }
>
Hello,
in the above example, what will happen if we add 'delete p;' at the end
of main(). What will be deleted ? Only the base class part or both base
and derived part of instantied object ?
thanks
Xavier