Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Misuses of ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 34 Topic 9585 of 9831
Post > Topic >>

Re: Misuses of RTTI

by Pavel Minaev <int19h@[EMAIL PROTECTED] > May 10, 2008 at 05:52 AM

On 10 ???, 07:13, OuFeRRaT <oufer...@[EMAIL PROTECTED]
> wrote:
> "RTTI should only be used sparingly in C++ programs. There are several
> reasons for this. Most im****tantly, other language mechanisms such as
> polymorphism and templates are almost always superior to RTTI. As with
> everything, there are exceptions, but the usual rule concerning RTTI
> is more or less the same as with goto statements. Do not use it as a
> shortcut around proper, more robust design. Only use RTTI if you have
> a very good reason to do so and only use it if you know what you're
> doing." as cited inhttp://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI
>
> I don't understand the sentence:
> "but the usual rule concerning RTTI is more or less the same as with
> goto statements" can anyone explain it?

The usual rule concerning goto is "don't use it unless you _know_ it's
the most clear way to achieve what you're trying to do here". This
phrase you've cited suggests that the same is applicable to RTTI.

> Furthermore, can we talk about the missues of RTTI?

Well, the most typical anti-pattern is using a series of if-else with
typeid in conditionals:

class object {...};
class foo : public object {...};
class bar : public object {...};
....

void do_something(object* obj)
{
   if (typeid(*obj) == typeid(foo))
   {
     foo* foo = static_cast<foo*>(obj);
     // do foo-specific processing here
     ...
   }
   else if (typeid(*obj) == typeid(bar))
   {
     bar* bar = static_cast<bar*>(obj);
     // do bar-specific processing here
     ...
   }
}

The usual advice in some cases is to use polymorphism - virtual
functions (and, if needed, abstract base cl*****) instead.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 34 Posts in Topic:
Misuses of RTTI
OuFeRRaT <ouferrat@[EM  2008-05-09 21:13:35 
Re: Misuses of RTTI
Pavel Minaev <int19h@[  2008-05-10 05:52:08 
Re: Misuses of RTTI
Mathias Gaunard <loufo  2008-05-10 05:53:41 
Re: Misuses of RTTI
brangdon@[EMAIL PROTECTED  2008-05-10 06:14:41 
Re: Misuses of RTTI
Francis Glassborow <fr  2008-05-10 06:14:40 
Re: Misuses of RTTI
MiB <Michael.Boehnisch  2008-05-10 14:08:42 
Re: Misuses of RTTI
"Bo Persson" &l  2008-05-11 16:40:58 
Re: Misuses of RTTI
Carl Barron <cbarron41  2008-05-11 22:13:29 
Re: Misuses of RTTI
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-13 10:55:29 
Re: Misuses of RTTI
galathaea <galathaea@[  2008-05-13 21:12:13 
Re: Misuses of RTTI
Gerhard Fiedler <gelis  2008-05-13 21:27:28 
Re: Misuses of RTTI
bravo <szymon.gatner@[  2008-05-14 08:36:57 
Re: Misuses of RTTI
brangdon@[EMAIL PROTECTED  2008-05-17 02:27:45 
Re: Misuses of RTTI
Tony Delroy <tony_in_d  2008-05-14 08:38:14 
Re: Misuses of RTTI
Mathias Gaunard <loufo  2008-05-14 12:27:28 
Re: Misuses of RTTI
Mathias Gaunard <loufo  2008-05-14 12:55:43 
Re: Misuses of RTTI
anthonypon@[EMAIL PROTECT  2008-05-15 02:47:07 
Re: Misuses of RTTI
Greg Herlihy <greghe@[  2008-05-15 11:21:05 
Re: Misuses of RTTI
dizzy <dizzy@[EMAIL PR  2008-05-15 11:57:29 
Re: Misuses of RTTI
dizzy <dizzy@[EMAIL PR  2008-05-15 11:57:29 
Re: Misuses of RTTI
Edward Diener <diener8  2008-05-15 15:57:15 
Re: Misuses of RTTI
Mathias Gaunard <loufo  2008-05-16 17:30:34 
Re: Misuses of RTTI
Frank Birbacher <blood  2008-05-16 17:44:13 
Re: Misuses of RTTI
Daniel James <wastebas  2008-05-16 17:43:20 
Re: Misuses of RTTI
"Marcin Kalicinski&q  2008-05-17 02:57:29 
Re: Misuses of RTTI
Edward Diener <diener8  2008-05-17 14:38:55 
Re: Misuses of RTTI
Greg Herlihy <greghe@[  2008-05-17 14:36:47 
Re: Misuses of RTTI
Edward Diener <diener8  2008-05-17 14:39:16 
Re: Misuses of RTTI
Andre Kaufmann <akfmne  2008-05-17 14:38:10 
Re: Misuses of RTTI
Andre Kaufmann <akfmne  2008-05-17 23:20:59 
Re: Misuses of RTTI
Daniel James <wastebas  2008-05-18 16:48:55 
Re: Misuses of RTTI
Andre Kaufmann <akfmne  2008-05-19 06:05:40 
Re: Misuses of RTTI
Francis Glassborow <fr  2008-05-19 12:01:47 
Re: Misuses of RTTI
Francis Glassborow <fr  2008-05-20 18:59:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:02:34 CDT 2008.