Archived

This topic is now archived and is closed to further replies.

Yanroy

RTTI broke my project?

Recommended Posts

I''m devving for WinCE, and I couldn''t find the option on any of the dialogs to enable RTTI like there is for Win32, so I went over to my normal VC++, found that RTTI = /GR, and then added /GR to the list of parameters in the WinCE VC++. Now strange things happen and I can''t seem to undo it. It thinks /GR is still specified, but it isn''t anywhere I can find it. I even searched through the project file for it. Any ideas? Here''s the errors and the code that cause them:
if (typeid(*Device) == typeid(CStampCtrl))

// C:\Documents and Settings\u4ra9rm3\Desktop\Code\Winter Robot\StampCtrl.cpp(410) : warning C4541: ''typeid'' used on polymorphic type ''class CSerialDevice'' with /GR-; unpredictable behavior may result
//C:\Documents and Settings\u4ra9rm3\Desktop\Code\Winter Robot\StampCtrl.cpp(410) : error C2678: binary ''=='' : no operator defined which takes a left-hand operand of type ''const class type_info'' (or there is no acceptable conversion)
 
Of course, I only did this because I was having that second error before I added the /GR. According to MSDN, there''s an operator == that works, but as you can see, it doesn''t. What should I do? --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
I don''t use MSVC, but I''m gonna venture a guess that your warning is caused by using typeid on a polymorphic class without RTTI enabled, thus it can''t properly determine the type.

I don''t know about that error though...

Share this post


Link to post
Share on other sites
Well, does anyone know how to turn it back on? I thought I was turning it on the first time, but maybe it defaults to on.

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
Uh it can''t be on with an error message like that, especially since it mentions /GR-.

You have to
#include <typeinfo> to use the type_info class. Read Stroustrup''s discussion of RTTI.   

Share this post


Link to post
Share on other sites
well, we have (almost) the same problem.. We couldn''t find how to enable RTTI in VC Embedded (6.0). Then similarly, we tought about adding /GR option manually (still from the settings menu). It recompiles, but deletes the /GR option automatically. So, as soon as you put the /GR option and click OK and reopen it''s gone.

So, probably, you too couldn''t enable RTTI as we couldn''t. And yes, msdn (for embedded) have RTTI explained.. but not the way to enable..

Is there anybody out there who knows the answer?

Share this post


Link to post
Share on other sites
I just executed the compiler with the /HELP option... and it lists /GR[-] as an unsupported feature (but I'm using embedded v3). How am I to live without RTTI?

EDIT: If I check a base-class pointer address against a derived class pointer address, will they be the same if the base class pointer is the same object as the derived one? I know that's confusing so here's what I mean:

class A;
class B : public A;

B *Derived = new B;
A *Base = Derived;

if (Base == Derived)
{
// instant dynamic cast, if I have an object to check against
}


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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

[edited by - Yanroy on August 14, 2003 11:27:16 AM]

Share this post


Link to post
Share on other sites
a) If your class doesn't contain a virtual member function, you won't get RTTI, only static checking
b) Before being allowed to use typeid, you must #include <typeinfo>
c) dynamic_cast is preferable to typeid in 90% of cases, and in all cases where you're doing a type check prior to a cast...

if( typeid(*Device) == typeid(CStampCtrl) )
((CStampCtrl*)Device)->SomeMemberFunction();

becomes

CStampCtrl* ptr = dynamic_cast<CStampCtrl*>(Device);
if (ptr != 0) ptr->SomeMemberFunction();


typeid only checks for exact type matches.
dynamic_cast does a safe base-to-derived cast and returns 0 if the cast is impossible ( or throws an std::bad_cast if you were using references )


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on August 14, 2003 1:02:27 PM]

Share this post


Link to post
Share on other sites
I have a base class with several objects derived from it. One of these derived classes has a member function that takes a pointer to an instance of the base class. I need some way of ensuring that the base class pointer being passed into the function isn''t in reality another one of that same derived class (because then I''d end up in an infinte recusion). Don''t tell me my heirarchy hasn''t been thought out well... this is the only logical way, since these objects are virtual counterparts to physical peripherals, and one of these peripherals controls which others will be used.

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
So you're trying to protect the programmer from themselves?

Edit: virtual equivalents to real-life devices are immediately recognizable, but not the only way of doing things.

[edited by - antareus on August 14, 2003 1:21:06 PM]

Share this post


Link to post
Share on other sites
Why do you instantly I don''t know what I''m doing? No, I''m not trying to protect me from myself, I''m trying to protect my application framework from self-distructing. Because this controller device is just another device, it will be activated just like any other. But this one''s activate function is only used to redirect activation to another device. Thus if it is activated directly, it will try to activate itself, over and over and over. All the other devices call the activate function of the controller device when they wish to be activated. This isn''t the issue at all... I just need something akin to type checking or dynamic cast. Would my pseudocode above work? I can''t see a way of testing whether it works or not...

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Share this post


Link to post
Share on other sites
If you don''t have RTTI then just emulate it by having a virtual member function that returns a boolean describing whether that controller can process messages. In the base class it can be marked true and in that one class you can mark it false. This is kind of like a type field, which is another way of doing it.

Hope that helps!

Share this post


Link to post
Share on other sites