Solved

dynamic_cast

Posted on 2000-03-14
17
433 Views
Last Modified: 2012-06-27
Hi,
Dynamic casts are supposed to allow traversal of the inheritance graph. However they work for classes having virtual functions only. Why don't dynamic casts work for classes (inheritance graph) sans virtual functions.
For example :

double dDividend = dynamic_cast<double>
                   ( exDays * 2 );

will give an error.


Regards
Shayad
0
Comment
Question by:shayad
  • 6
  • 4
  • 3
  • +3
17 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2617288
maybe you didn't do the casting right

the idea is that you can make casting from father to son or father pointer to son pointer

the idea is that if you got a based1 class and a drived1 class

then you can
do
based1 *p1;
drived1 *p2;
.....
p1=dynamic_cast<based1*>p2;

that way you can make containers
that are an array of pointers to the father that you can cast to pointers to one of the sons

and if you can't make the cast the dynamic_cast will return NULL
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2617340
>> Why don't dynamic casts work for classes
>> (inheritance graph) sans virtual functions.
You don't KNOW that they won't work, however you are GUARANTEED that they will work.  The reason is that for this to work the compiler needs to place information inside the object that helps program figure out what the object actually is.  (Or at least that is how it is usually handled).  However if a class doesn't have virtual functions (and a few other restrictions) then the layout of the object must exactly match the layout produced by C so there is no room for this identification data.  So when you add virtual functions to a class you give the compile the freedom to add the data in needs.  In fact, in most compilers, the data it needs is actually a pointer to the virtual function table, i.e. the data is automatically there when you add the virtual functions.
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2617440
to nietod
you trying to say that it won't work without a virtual function in the calss??
0
 
LVL 22

Expert Comment

by:nietod
ID: 2617459
>> that it won't work without a virtual function in the calss??
It MIGHT not work if it doesn't have any virtual functions.   That is, the C++ standard says that the compiler doesn't have to make dynamic cast work on objects that don't have virtual functions.  However, if the class contains virtual functions (perhaps defined in a base class) then the compiler must make the dynamic cast work.
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2618636
here i won't agree with you when you enable RTTI the typeid and dynamic_cast should work
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2618886
I don't see where you disagree actually.
Borland will by default not add RTTI to POD like types, like nietod says, unless you tell it to do so anyway. But that basically changes the 'It MIGHT not work' into 'it will always work' by changing the compiler.
0
 

Author Comment

by:shayad
ID: 2619365
From the answers, I understand that dynamic_cast will work only when virtual functions are invloved. I wanted to know why wont they work when no virtual functions are present.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2619389
>> I wanted to know why wont they work
>> when no virtual functions are present.

For a dynamic cast to work, the computer needs some way of actually determining what type of object has been supplied to the dynamic cast.  In general, this is impossible to tell.  the dynamic cast just has a pointer to some location in memory, how can it tell what object is stored in tha location?  

One way that compilers can make it work is that they can place information  at the start of an object that helps it identify the object's type.  In that case the pointer suplied to dynamic cast will point to information that helps identify the object's type and allows the dynamic cast to function correctly.

However, the compiler is not always allowed to add to an object like that.  For example, the C++ standard specifically states that if an object has no virtual functions and no base classes, then a pointer to the object is also a pointer to the furst data member in the object.  (This is to help insure compatiblity with C structures).  So the compiler can't place this extra identification data in an object that has no virtual functons.   (Although apparently BCB does this if you elect it???)

Makes sense now?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2619398
Not sure this has been said. RTTI has to be stored somewhere, what better place then together with info on virtual functions (the VTable). You could seperate them off course, but that would not add more overhead and you may still have a problem where the langauge demands that the object layout is compatible with C structures.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2619467
Yes, usually the virtual table pointer points to the start of an array of function pointers and imediately before that table is the typeinfo structure.  Usually.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2619851
dynamic_cast requires runtime type identification (rtti)

rtti requires the type information be stored for each object.  This usually (read always) requires storing the information somewhere in the object

rtti is usually (read always) stored in the vtable (ie. it is a predefined virtual 'function').

turning on rtti simply always adds the rtti info to the vtable (if any) of object.

if there are no virtual functions already, then there is no vtable and C++ will not add one, so there is no dynamic runtime type information.

that means only the static type is available.


for the example you give, dynamic cast is not appropriate anyway because there are no classes involved.

what you want here is a static_cast (or a function-style cast is good ie:)

double dDividend = double(exDays*2);

0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2620832
sorry for my last comment you're right nietod

0
 
LVL 22

Expert Comment

by:nietod
ID: 2620885
No applogy needed.
0
 
LVL 3

Expert Comment

by:Try
ID: 2622964
You don't KNOW that they won't work, however you are GUARANTEED that they will work.

------------------------------------

Brilliant!!   Just simply brilliant!!


On the surface it may look contradictory, but upon closer examination, it is actually repetitive (not to mention, FALSE).

------------------------------------

To say something is FALSE, is saying, it is NOT TRUE.

Therefore, to say something is NOT(NOT TRUE), is saying, it is TRUE.

(Sort of like,  -(-x) = +x  or simply  x)

Therefore, to say, 'You don't know(they won't work)' is like saying, 'You know they will work'.

Therefore, if you know they will work, there is no need to repeat "however you are GUARANTEED that they will work."

The entire statement then reads, "You know they will work, however you are GUARANTEED that they will work."

But since the beginning of the statement asserts that, "You know they will work," there is no included statement of condition under which you know (for sure) this will occur.

It doesn't say, "This will occur, if and only if, ... (the condition)"

Without a condition to validate the statement's truthfulness, the statement is automatically FALSE.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2623095
Try -- i think it was a typo. It thnk he was trying to say "you are not GUARANTEED that they will work".

0
 
LVL 22

Expert Comment

by:nietod
ID: 2623185
Yes, it was definitely a typo!!  I just missed the "not" which is a mere three letters and a world of difference.

All this stuff about trues and false is interesting.  But I've never seen anything like it before.  Do you think it could be useful to me as a programmer?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2623210
not :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
object modeling - load and save functions 5 59
Unable to start eclipse ? 17 128
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 90
What is sub-make ? 2 37
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now