469,091 Members | 1,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,091 developers. It's quick & easy.

Strange dynamic_cast problem

Hello, I have an object called XXX previously derived from CDocument in
my MDI project.

Later, I create an concrete class called Subject. And I let XXX to have
multiple inheritance from Subject (Subject is an object with protected
constructor)

The problem is, whenever I used dynamic_cast to cast a Subject pointer
to XXX (I am quite sure Subject pointer is actually pointing to XXX), a
visual c++ runtime error will occur.

void fun(Subject *s) {
XXX *var = dynamic_cast(s);
var->hehehe();
}

However, when I try to do this:

void fun(Subject *s) {
XXX *var = (XXX *)(s);
var->hehehe();
}

everything just go fine. may i noe why this happen? is this bug related
to something called dynamic creation?

thanks you.

cheok

Jul 23 '05 #1
2 1639
yc*****@gmail.com wrote:
Hello, I have an object called XXX previously derived from CDocument in
my MDI project.

Later, I create an concrete class called Subject. And I let XXX to have
multiple inheritance from Subject (Subject is an object with protected
constructor)
Hmm, does that mean something like:

class XXX : public Subject, public CDocument

? Sometimes a single line of code can say more than a thousand words.
The problem is, whenever I used dynamic_cast to cast a Subject pointer
to XXX (I am quite sure Subject pointer is actually pointing to XXX),
Well, dynamic_cast is the one to use if you are not sure.
a visual c++ runtime error will occur.
In which line?
void fun(Subject *s) {
XXX *var = dynamic_cast(s);
That'd be a syntax error. dynamic_cast expects a type to cast to.
var->hehehe();
You didn't check whether var is a null pointer or not. If the dynamic_cast
fails, the returned value is a null pointer, in which case the above line
would probably crash.
}

However, when I try to do this:

void fun(Subject *s) {
XXX *var = (XXX *)(s);
var->hehehe();
}

everything just go fine. may i noe why this happen?
Hard to say with the small amount of (pseudo-)code you showed.
is this bug related to something called dynamic creation?


No idea what you mean by "something called dynamic creation".

Jul 23 '05 #2

<yc*****@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hello, I have an object called XXX previously derived from CDocument in
my MDI project.

Later, I create an concrete class called Subject. And I let XXX to have
multiple inheritance from Subject (Subject is an object with protected
constructor)

The problem is, whenever I used dynamic_cast to cast a Subject pointer
to XXX (I am quite sure Subject pointer is actually pointing to XXX), a
visual c++ runtime error will occur.

void fun(Subject *s) {
XXX *var = dynamic_cast(s);
var->hehehe();
}


XXX *var = dynamic_cast<XXX*>(s);

[snip]
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Thomas Lorenz | last post: by
15 posts views Thread by Grizlyk | last post: by
15 posts views Thread by Bo Yang | last post: by
18 posts views Thread by Eric | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.