Boost logo

Boost :

From: Jody Hagins (jody-boost-011304_at_[hidden])
Date: 2005-03-01 12:54:56


On Tue, 1 Mar 2005 09:31:21 -0800 (PST)
Chris Just <cpjust_at_[hidden]> 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).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk