dynamic_cast to find out the deleted pointer

G

Ganesh

On devx site, I saw following code. It says when a derived class is
tried to cast to base type, it looks at the missing vtable and
complains if the object is already deleted.
I am of the opinion that this doesnt work if the destructor is not
virtual or when the class has no virtual members. I would like to know
is there anything wrong in what I am thinking.
(I agree it is better to keep base class dtor as virtual, but supposing
it is not virtual, does following method work to detect a deleted
pointer ?)

Thanks
Ganesh

#include <iostream>

using namespace std;

class A
{
public:
A() {}
virtual ~A() {}
};

class B : public A
{
public:
B() {}
};

int main()
{
B* pB = new B;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;


delete pB;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;


}

the output:
dynamic_cast<B*>( pB) worked
dynamic_cast<B*>( (A*)pB) worked
dynamic_cast<B*>( pB) worked
dynamic_cast<B*>( (A*)pB) failed
 
G

Gianni Mariani

Ganesh wrote:
....
(I agree it is better to keep base class dtor as virtual, but supposing
it is not virtual, does following method work to detect a deleted
pointer ?)

Nothing knows what a deleted pointer is.

....
B* pB = new B;

cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;


delete pB;

Everything referencing pB after this point is undefined behaviour.
You're lucky it does not melt your CPU.
cout << "dynamic_cast<B*>( pB) ";
cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl;

cout << "dynamic_cast<B*>( (A*)pB) ";
cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl;
....
 
H

Howard

Gianni Mariani said:
Ganesh wrote:
...

Nothing knows what a deleted pointer is.

...

Everything referencing pB after this point is undefined behaviour. You're
lucky it does not melt your CPU.

A wee bit dramatic there, wouldn't you say? I should hope that undefined
behavior doesn't lead to anything quite *that* drastic! If it does, I'd
better do a LOT more work on validating my code before I run any unit tests!
And can you imagine the tech support calls? "I found a bug in your new
software... could you send someone over to put out the fire, please?" :)


-Howard
 
R

red floyd

Howard said:
A wee bit dramatic there, wouldn't you say? I should hope that undefined
behavior doesn't lead to anything quite *that* drastic! If it does, I'd
better do a LOT more work on validating my code before I run any unit tests!
And can you imagine the tech support calls? "I found a bug in your new
software... could you send someone over to put out the fire, please?" :)

Yeah, he's dramatic, but technically, Gianni is correct. Once you
invoke UB, anything *can* happen. It's highly unlikely that some of the
possible effects (such as causing the sun to go nova, turning lead into
gold, etc...) will happen, but the standard does not forbid them :)
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top