473,395 Members | 1,411 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,395 software developers and data experts.

About RTTI

Hi all

As far as i know, there has 2 ways RTTI in C++
one is dynamic_cast and another is typeid

Since, my book only pointed me that to use "typeid" and "static_cast"
conjunction will be much more efficient than "dynamic_cast",
but what the book does not provide any evidence.

So my question can any explain to me why dynmic_cast is slower than typeid??

And since it's much slower, why not simply throw it away
Any Help will be appreciated

Jul 19 '05 #1
4 3853
"Steven Lien" <st********@yahoo.com> wrote in message
news:bh**********@news.ethome.net.tw...
Hi all

As far as i know, there has 2 ways RTTI in C++
one is dynamic_cast and another is typeid

Since, my book only pointed me that to use "typeid" and "static_cast"
conjunction will be much more efficient than "dynamic_cast",
but what the book does not provide any evidence.
I don't think that's true in general. In the example given in your book,
is the pointer type-checked once then used many times? If so, a
static_cast to the known object type will be faster than repeated
dynamic_casts. But I would use dynamic_cast to perform the check
in the beginning, unless there were a good reason not to.
So my question can any explain to me why dynmic_cast is slower than typeid??
And since it's much slower, why not simply throw it away

Any Help will be appreciated

Jul 19 '05 #2
"Steven Lien" <st********@yahoo.com> wrote...
As far as i know, there has 2 ways RTTI in C++
one is dynamic_cast and another is typeid

Since, my book only pointed me that to use "typeid" and "static_cast"
conjunction will be much more efficient than "dynamic_cast",
but what the book does not provide any evidence.
Could it be because it's really nonsense?
So my question can any explain to me why dynmic_cast is slower than typeid??

No. Because there is no such requirement or any evidence of that
in the language definition. You have to ask the authors of the
book you're referring to.
And since it's much slower, why not simply throw it away


Again, I am not sure where you got that "much slower" nonsense,
but I believe they both are in the language because they serve
different purposes.

Victor
Jul 19 '05 #3

Steven Lien <st********@yahoo.com> wrote in message
news:bh**********@news.ethome.net.tw...
Hi all

As far as i know, there has 2 ways RTTI in C++
one is dynamic_cast and another is typeid

Since, my book only pointed me that to use "typeid" and "static_cast"
conjunction will be much more efficient than "dynamic_cast",
but what the book does not provide any evidence.


Which book and author? Perhaps you need a better one.

-Mike

Jul 19 '05 #4
"Steven Lien" <st********@yahoo.com> wrote in message
news:bh**********@news.ethome.net.tw...
| As far as i know, there has 2 ways RTTI in C++
| one is dynamic_cast and another is typeid
|
| Since, my book only pointed me that to use "typeid" and "static_cast"
| conjunction will be much more efficient than "dynamic_cast",
| but what the book does not provide any evidence.
|
| So my question can any explain to me why dynmic_cast is slower than
typeid??
|
| And since it's much slower, why not simply throw it away

Slower at what ? They serve very different purposes.
Consider:

#include <typeinfo>

class One { public: virtual ~One(){} };
class Two : public One {};
class Three : public Two {};

void isThisATwo(One* p)
{
// which one of the following values do you want ???
bool same1 = ( 00 != dynamic_cast<Two*>(p) );
bool same2 = ( typeid(*p)==typeid(Two) );
}

int main()
{
Three p;
isThisATwo(&p); // which result do you want?
}

dynamic_cast does a more exhaustive search, to tell you
whether an instance is of a specific type, *OR* any type
derived from it. typeid() cannot provide this information.

Additionally, dynamic_cast can perform casts that are
not accessible to static_cast:
class Base1 { public: virtual ~Base1(){} };
class Base2 { public: virtual ~Base2(){} };
class Derived : public Base1, public Base2 {};

void f(Base1* p1)
{
// can't be done with a static_cast...
Base2* p2 = dynamic_cast<Base2*>(p1);
}

int main()
{
Derived d;
f( &d );
}
Make sure to read other books about C++...
Regards,
--
Ivan Vecerina <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com



Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rick | last post by:
Hi, I wrote a few classes recently and am writing a small GC implementation for C++. I will be implementing my own gc_ptr type maintain a list where I'll store pointers to allocated memory. I...
2
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return...
9
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This...
2
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram...
10
by: Neo | last post by:
what is RTTI? and how to implements it? give me sourceful link for learn and implements it (using standard C mean portable). regards, -aims
5
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined...
33
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; ...
2
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks...
6
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt support this, but I dont know if gcc can , ...
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: 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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.