Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Should I use RTTI?

Reply
Thread Tools

Should I use RTTI?

 
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-20-2006
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 };

Type GetType() { return type_; }

private:
Type type_;
}

class B : public A { ... }

and now if I want to know what type of class it is I just call a
GetType method.

I'd be really pleased with any advice. Thanks.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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 };
>
> Type GetType() { return type_; }
>
> private:
> Type type_;
> }
>
> class B : public A { ... }
>
> and now if I want to know what type of class it is I just call a
> GetType method.
>
> I'd be really pleased with any advice. Thanks.
>

Perfectly valid for a known set of types.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-20-2006
Ian Collins wrote:
> (E-Mail Removed) wrote:
>
>>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 };
>>
>> Type GetType() { return type_; }
>>
>> private:
>> Type type_;
>>}
>>
>>class B : public A { ... }
>>
>>and now if I want to know what type of class it is I just call a
>>GetType method.
>>
>>I'd be really pleased with any advice. Thanks.
>>

>
> Perfectly valid for a known set of types.
>

I forgot to point out that GetType() should be virtual.

--
Ian Collins.
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      12-20-2006
(E-Mail Removed) wrote:
> 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 };
>
> Type GetType() { return type_; }
>
> private:
> Type type_;
> }
>
> class B : public A { ... }
>
> and now if I want to know what type of class it is I just call a
> GetType method.


Your method requires some type of maintenance by the programmer. The
built-in typeid facility does all the magic for you.

I suppose you could build somthing similar out of templates but then "why" ?
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      12-20-2006

(E-Mail Removed) wrote:
> 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 };
>
> Type GetType() { return type_; }
>
> private:
> Type type_;
> }
>
> class B : public A { ... }
>
> and now if I want to know what type of class it is I just call a
> GetType method.
>
> I'd be really pleased with any advice. Thanks.


Is this what you had in mind?

#include <iostream>
#include <ostream>
#include <typeinfo>

template< typename Type >
class A {
Type type;
public:
A() : type() { }
virtual ~A() { }
std::string GetType() const { return typeid(type).name(); }
virtual void foo() const = 0;
};

template< typename T >
class B : public A< T >
{
public:
void foo() const
{
std::cout << "B< " << A<T>::GetType();
std::cout << " >::foo()\n";
}
};

// lets replace X, Y and Z with int, long and std::string for for
illustration.

int main()
{
B< int > bn;
bn.foo();
B< long > bl;
bl.foo();
B< std::string > bs;
bs.foo();
}

/* possible sample output:
B< i >::foo()
B< l >::foo()
B< Ss >::foo()
*/

Then why use RTTI? The only reason i can think for getting the typeid
involved is to break the rules of inheritance. These examples with int,
long and std::string could have been X, Y and Z. If you needed B<
std::string > to call a different foo() than the templated version
provided, specialize the template. Its that simple.

template< >
void B< std::string >::foo() const
{
std::cout << "the member in base is a string.\n";
}

/* result:
B< i >::foo()
B< l >::foo()
the member in base is a string.
*/

You don't need RTTI to do that. There is no need to tell the compiler
which type is involved, it already knows.

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      12-20-2006

(E-Mail Removed) wrote:
> 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...


I am of the opinion that you should disable all possible language
features you can and rewrite them all yourself. Disable RTTI, disable
exceptions, and don't dare use the standard library...especially not
the STL.

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      12-21-2006
(E-Mail Removed) wrote:

> 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...


I think, RTTI used for "dynamic_cast<>", not only for "typeid" (at
least for some C++ compiler implementation it is true - "dynamic_cast
.... Note: Runtime type information (RTTI) is required for
dynamic_cast").

If you are using class with multiple inheritance and pointers to its
base classes (for example pointers returned from base classes), you
especially can not avoid "dynamic_cast<>", due to unpredicted "this"
address migration from one base class to another or compiler
limitations.

Consider output

//base pointers are not equal
= Test* new Test(1)
9f640: Class
9f630: Base: data: 1
9f638: Class2
9f630: Test: data: 1
= Class* new Test(0)
9fef0: Class
9fee0: Base: data: 0
9fee8: Class2
9fee0: Test: data: 0

= Test* dynamic_cast<Test*> (Class*) ( from base class to real class
of obj )
9fee0: ok 9fee0=dynamic_cast<Test*>(9fef0)

= return "this" from base class Class
9fee0: 9fee0->test() == 9fef0
* Test* C-style cast<Test*> (Class*) ( from base class to real class
of obj )
error: cannot convert from base 'Class' to derived type 'Test' via
virtual base 'Class'

= return "this" from base class Class2
9fee0: test2() == 9fee8
* Test* C-style cast<Test*> (Class2*) ( from base class to real class
of obj )
9fee0: (C-style cast) from 9fee8

As you see, you can write C-style cast for not virtual base classes,
but for multiple inheritance it will be not only unclear (what the way
compiler will use - reinterpret_cast<> or dynamic_cast<>?), it is very
easy to put wrong data to the class pointer without dynamic_cast<>.

 
Reply With Quote
 
Craig Scott
Guest
Posts: n/a
 
      12-21-2006
On Dec 21, 4:54 am, (E-Mail Removed) wrote:
> 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 };
>
> Type GetType() { return type_; }
>
> private:
> Type type_;
>
> }class B : public A { ... }
>
> and now if I want to know what type of class it is I just call a
> GetType method.
>
> I'd be really pleased with any advice. Thanks.



Something a little out of left field may be relevant to you here. If
you are using multiple modules in your application (ie building DLL's
or shared libraries), then using RTTI and/or dynamic_cast can be a
little shaky. If two objects of the same type are created in different
modules/DLLs/shared libraries, then it is entirely possible that the
application will see them as two distinct types with different RTTI
id's. Since the standard is silent about situations like this (it says
nothing about multi-module programs), compilers are free to do this and
indeed we have encountered some mainstream compilers which did (eg some
versions of GCC, but later versions have fixed this). A dynamic_cast
can also incorrectly fail if the object was created in a different
module, and again we have observed this in practice too.

So I guess if you are really paranoid, then your home-grown approach
will avoid these problems, but at the cost of a higher maintenance
burden and more fragile code.

For reference, you may also want to check out two other threads related
to the issues above:

http://groups-beta.google.com/group/...feaa6e/?hl=en#

http://groups-beta.google.com/group/...83565c227fd25b

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      12-21-2006

Craig Scott wrote:

> If two objects of the same type are created in different
> modules/DLLs/shared libraries, then it is entirely possible that the
> application will see them as two distinct types with different RTTI
> id's.


It is not C++ bug. It is compiler bug, it is evidently. RTTI means
"type identification", not RTMI "module identification". If you are
using constant class description (for example, read-only header file
without preprocessor) in each module, any may expect equal runtime
class. In fact, your words mean, that you can not use many C++ features
for the compiler.

There are many other bugs or not complete C++ implementation in
existing compilers.

>Since the standard is silent about situations like this (it says
> nothing about multi-module programs), compilers are free to do this and

Write to C++ standart org. If it is real trouble and all around do not
understand, it can be corrected.

> A dynamic_cast
> can also incorrectly fail if the object was created in a different
> module
>
> So I guess if you are really paranoid, then your home-grown approach


But it is not easy to understand what do you advise to do instead of
dynamic_cast<>, typeid and try/catch/throw

 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      12-21-2006
(E-Mail Removed) wrote:

> 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 };
>
> Type GetType() { return type_; }
>
> private:
> Type type_;
> }
>
> class B : public A { ... }
>
> and now if I want to know what type of class it is I just call a
> GetType method.
>
> I'd be really pleased with any advice. Thanks.


I suggest you (a) don't use RTTI and (b) don't use the above either.
Such use violates "tell don't ask". The whole point of inheritance is so
you can use an object without knowing its exact type.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
should I use java.awt.* only or also use javax.swing in applets? steveh44 Java 12 05-06-2011 05:06 PM
Builtn super() function. How to use it with multiple inheritance? Andwhy should I use it at all? Lacrima Python 43 08-02-2010 09:28 PM
I use IE right now--Should I use Firefox or Mozilla? BOB Firefox 10 04-16-2006 08:23 PM
taking 70-290 should i be scared? What should i expect??? Raymond Munyan MCSE 31 12-01-2004 02:34 PM
How should control images should be handled? ~~~ .NET Ed ~~~ ASP .Net Building Controls 1 11-03-2004 12:30 PM



Advertisments