473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1737
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. The question I would to rise is when...
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. Invalid casts of pointers give a '0'-result. As...
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 code (which compiles well with my BCB 6.0 compiler)....
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 from Level1 which derives from Base. If you look...
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 virtual inheritance to implement interfaces. I ran...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
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 works? Could you please explain how for me?...
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 is happening myself. I am hoping that the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.