Strange dynamic_cast problem

Y

yccheok

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
 
R

Rolf Magnus

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".
 
J

Jim Langston

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]
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top