P
Paul McKenzie
Consider the following:
class A
{
public:
A() { }
~A() { }
};
class B : public A
{
public:
B() { }
virtual ~B() { }
};
class C : public B
{
public:
C() { }
};
int main()
{
B *pB = new C;
delete pB;
}
I am having a debate of whether the "delete pB" invokes undefined behavior.
I believe it does invoke undefined behavior.
However someone else has claimed that since the base pointer defined in
main() is to a B, and B has a virtual destructor, deleting
through a pointer to B, is safe (no undefined behavior).
My claim is that regardless of whether B has a virtual destructor, the base
class A, has a non-virtual destructor,
therefore the code invokes undefined behavior.
Who is correct in this regard?
Paul
class A
{
public:
A() { }
~A() { }
};
class B : public A
{
public:
B() { }
virtual ~B() { }
};
class C : public B
{
public:
C() { }
};
int main()
{
B *pB = new C;
delete pB;
}
I am having a debate of whether the "delete pB" invokes undefined behavior.
I believe it does invoke undefined behavior.
However someone else has claimed that since the base pointer defined in
main() is to a B, and B has a virtual destructor, deleting
through a pointer to B, is safe (no undefined behavior).
My claim is that regardless of whether B has a virtual destructor, the base
class A, has a non-virtual destructor,
therefore the code invokes undefined behavior.
Who is correct in this regard?
Paul