472,328 Members | 1,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 1704
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: GianGuz | last post by:
Everyone knows about the complex and cpu-expensive procedures taken by dynamic_cast to find the right function call in a virtual classes hierarchy....
8
by: Thomas Lorenz | last post by:
Hello, first, I didn't find any reference to my question through googling. dynamic_cast uses RTTI to determine if the cast expression is valid....
5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean...
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives...
5
by: mijobee | last post by:
Hello Everyone, I just wanted to check that I'm using dynamic_cast correctly. I have a hierarchy of objects using pure virtual classes and...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that ...
15
by: Bo Yang | last post by:
Hi, I can understand static_cast, reinterpret_cast and const_cast, they all work at compile time. But I can figure out how the C++'s dynamic- cast...
18
by: Eric | last post by:
Ok...this seems to be treading into some really esoteric areas of c++. I will do my best to explain this issue, but I don't fully understand what...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.