Re: Re: Is there any way to get typeid() to return the derived class name?

Jody Hagins wrote:
I've been looking through the boost library and I haven't been able to find anything that works like typeid() that returns the exact (i.e. derived) class name instead of just the base class. I want to compare two derived classes to make sure that they are of the same class, but typeid() only returns the name of the base class.
If there is a boost function that does this, or if someone has a way of doing it could you please point me in the right direction?
Funny about timing...
I think I can answer your question, but I must answer with another question. What do you mean by "typeid() only returns the name of the base class?" This is surely not the case for polymorphic types, at least according to the standard. Maybe if you post a bit of code, which represents what you are trying to accomplish, I can help...
I'm using MS Visual C++ 6.0. This is what I'm doing: CBase { ... }; CDerived : public CBase { ... }; CSomething { public: CSomething( const CBase& base ) : m_Base( base ) {} CSomething& operator=( const CSomething& rhs ) { if ( typeid( m_Base ) != typeid( rhs.m_Base ) ) // This never fails. { throw exception(); } } private: CBase& m_Base; }; When I look at the type_info object's name(), it always returns "CBase", even if it's actually a CDerived object. __________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250

On Tue, 1 Mar 2005 09:31:21 -0800 (PST) Chris Just <cpjust@yahoo.com> wrote:
I'm using MS Visual C++ 6.0. This is what I'm doing:
Sorry ;-)
CBase { ... }; CDerived : public CBase { ... };
CSomething { public: CSomething( const CBase& base ) : m_Base( base ) {}
CSomething& operator=( const CSomething& rhs ) { if ( typeid( m_Base ) != typeid( rhs.m_Base ) ) // This never fails.{ throw exception(); } }
private: CBase& m_Base; };
When I look at the type_info object's name(), it always returns "CBase", even if it's actually a CDerived object.
You have ommitted the most important pieces in your code sample... #include <typeinfo> #include <cassert> struct np_base { }; struct np_derived : public np_base { }; struct base { virtual ~base() { } }; struct derived : public base { }; int main(int, char *[]) { np_derived npd; derived d; np_base * np_base_ptr = &npd; base * base_ptr = &d; assert(typeid(*np_base_ptr) == typeid(np_base)); assert(typeid(*base_ptr) == typeid(derived)); return 0; } Compile and run the above on your compiler. If it fails, you have a problem with your compiler. Otherwise, you probably have a problem with your code. Note that you only get the most derived type, if the class is polymorphic (i.e., has at least one virtual function).

Chris Just <cpjust@yahoo.com> writes:
Jody Hagins wrote:
I've been looking through the boost library and I haven't been able to find anything that works like typeid() that returns the exact (i.e. derived) class name instead of just the base class. I want to compare two derived classes to make sure that they are of the same class, but typeid() only returns the name of the base class.
If there is a boost function that does this, or if someone has a way of doing it could you please point me in the right direction?
Funny about timing...
I think I can answer your question, but I must answer with another question. What do you mean by "typeid() only returns the name of the base class?" This is surely not the case for polymorphic types, at ^^^^^^^^^^^ least according to the standard. Maybe if you post a bit of code, which represents what you are trying to accomplish, I can help...
I'm using MS Visual C++ 6.0. This is what I'm doing:
CBase { ... }; CDerived : public CBase { ... };
A polymorphic type has at least one virtual function. HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
Chris Just
-
David Abrahams
-
Jody Hagins