 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jupiter Guest
|
Posted: Mon Aug 23, 2004 10:35 am Post subject: RTTI |
|
|
where is RTTI heavily used (in template or in virtual class) ??
[consider coding level not compiler level]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stefan Heinzmann Guest
|
Posted: Mon Aug 23, 2004 10:16 pm Post subject: Re: RTTI |
|
|
Jupiter wrote:
| Quote: | where is RTTI heavily used (in template or in virtual class) ??
[consider coding level not compiler level]
|
What is a virtual class? Do you mean classes with virtual functions?
Well, RTTI is only available in classes with at least one virtual
function, so your question sort of answers itself.
--
Cheers
Stefan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Mon Aug 23, 2004 10:30 pm Post subject: Re: RTTI |
|
|
[email]pravin.shelar (AT) rediffmail (DOT) com[/email] (Jupiter) writes:
| Quote: | where is RTTI heavily used (in template or in virtual class) ??
[consider coding level not compiler level]
|
In classes that have virtual functions. All class which have virtual
functions support RTTI. Classes which do not have virtual
functions do not support RTTI.
RTTI and templates are independent; if a class template has no
virtual functions, it does not support RTTI. If class template
*does* have virtual functions, it does support RTTI.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Roger Orr Guest
|
Posted: Tue Aug 24, 2004 10:49 pm Post subject: Re: RTTI |
|
|
"Jupiter" <pravin.shelar (AT) rediffmail (DOT) com> wrote
| Quote: | where is RTTI heavily used (in template or in virtual class) ??
[consider coding level not compiler level]
|
As other have said, in classes w/ virtual functions.
What difference does the answer make - ie what made you ask the question in
the first place?
Roger Orr
--
MVP in C++ at www.brainbench.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jive Guest
|
Posted: Fri Aug 27, 2004 2:49 am Post subject: Re: RTTI |
|
|
"llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote
| Quote: | pravin.shelar (AT) rediffmail (DOT) com (Jupiter) writes:
In classes that have virtual functions. All class which have virtual
functions support RTTI. Classes which do not have virtual
functions do not support RTTI.
|
How do you planned to implement down cast on class hierarchy were there
is no virtual functions and only trivial destructor?
AFAIK RTTI and dynamic binding are separate qualities not bound to each
other.
Furthermore, if RTTI is not used compiler may optimize it away, even if
there is virtual functions and base classes.
- jive
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sat Aug 28, 2004 2:45 am Post subject: Re: RTTI |
|
|
"jive" <jive (AT) nospam (DOT) invalid> writes:
| Quote: | "llewelly" <llewelly.at (AT) xmission (DOT) dot.com> wrote in message
news:86llg5kikk.fsf (AT) Zorthluthik (DOT) local.bar...
[email]pravin.shelar (AT) rediffmail (DOT) com[/email] (Jupiter) writes:
In classes that have virtual functions. All class which have virtual
functions support RTTI. Classes which do not have virtual
functions do not support RTTI.
How do you planned to implement down cast on class hierarchy were there
is no virtual functions and only trivial destructor?
|
$ cat dynamic_cast.cc
struct no_rtti_base{};
struct no_rtti_derived:no_rtti_base{};
struct rtti_base
{
virtual ~rtti_base(){}
};
struct rtti_derived:rtti_base{};
struct rtti_no_rtti_base:no_rtti_base
{
virtual ~rtti_no_rtti_base(){}
};
int main()
{
rtti_derived rd;
rtti_base* rbp= &rd;
//dynamic_cast<> permitted, because rtti_* has virtual
//function(s).
rtti_derived* rd2= dynamic_cast<rtti_derived*>(rbp);
no_rtti_derived nrd;
no_rtti_base* nrbp= &nrd;
//dynamic_cast<> is a compile time error, because no_rtti_base
//does not have virtual function(s)
no_rtti_derived* nrdp= dynamic_cast<no_rtti_derived*>(nrbp);
rtti_no_rtti_base rnrb;
no_rtti_base* nrbp2= &rnrb;
//dynamic_cast<> is a compile time error, because no_rtti_base
//does not have virtual function(s)
rtti_no_rtti_base* rnrbp= dynamic_cast<rtti_no_rtti_base*>(nrbp2);
}
$ g++ dynamic_cast.cc
dynamic_cast.cc: In function `int main()':
dynamic_cast.cc:29: error: cannot dynamic_cast `nrbp' (of type `struct
no_rtti_base*') to type `struct no_rtti_derived*' (source type is not
polymorphic)
dynamic_cast.cc:35: error: cannot dynamic_cast `nrbp2' (of type `struct
no_rtti_base*') to type `struct rtti_no_rtti_base*' (source type is not
polymorphic)
Without virtual functions, dynamic_cast<> is not allowed. Note that
this is not about how I plan to implement anything; it is what
the standard requires, and what many implementations have done
for years.
| Quote: | AFAIK RTTI and dynamic binding are separate qualities not bound to each
other.
|
This is not so in ISO C++. There are several compiliers which allow
one to use a compile-time flag which disables rtti for
*everything*, and still use virtual functions, but they are
non-conforming in that mode, because they do not provide
dynamic_cast at all.
| Quote: |
Furthermore, if RTTI is not used compiler may optimize it away, even if
there is virtual functions and base classes.
|
This is true, but to prove the RTTI for a type T is not used, the
compiler must analyze every module which uses any object of type
T, T*, T&, and cv-qualified variants thereof. That's not always
feasible; most implementations support shared libraries, which
either prevents said analysis entirely, or require it to be done
at the time the libraries are loaded. (Does anyone know of an
implementation which does optimizations at library-load time? It's
possible, I'm sure, but I don't know of any examples.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|