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

dynamic_cast to find out the deleted pointer

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

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


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

....
Jul 23 '05 #2

P: n/a

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:65********************@speakeasy.net...
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.


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


Jul 23 '05 #3

P: n/a
Howard wrote:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message

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?" :-)


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 :)
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.