23

I am attempting to do something like:

class Base {
public:
   Base() {
      cout << typeid(*this).name() << endl;
   }
   ...
};

class Derived : public Base { ... }
class MoreDerived : public Derived { ... }

Derived d;
MoreDerived m;

Problem is, I always get Base printed to the screen, when I need to see Derived and MoreDerived. Is there a way to get typeid to work this way with derived classes? Or is there another approach besides typeid?

Note: I am adding functionality to an already coded suite, so I don't want to have to add a virtual method to the base class where the derived classes return this value themselves. Also, not worried about runtime overhead, this will be part of a debug compile switch.

Cœur
  • 37,241
  • 25
  • 195
  • 267
steveo225
  • 11,394
  • 16
  • 62
  • 114

3 Answers3

17

In the constructor Base(), the object is still a "Base" instance. It will become a Derived instance after the Base() constructor. Try to do it after the construction and it will work.

See for example :

ysdx
  • 8,889
  • 1
  • 38
  • 51
  • Sadly, I have done this before. On the positive, I was really doing the print in the destructor along with stats, so I was able to save the name off. Thanks for the help. – steveo225 Jul 19 '11 at 14:21
  • @ysdx: it only works when `typeid(*this).name()` is put in a virtual function after the ctor. If `typeid(*this).name()` is put in a non-virtual function the printed out class is still `Base` – Steven Lee Sep 23 '21 at 04:36
  • @StevenLee, AFAIU as long as the Base class is polymorphic (you should really declare the Base::~Base virtual) this should return the dynamic type and not the static type. – ysdx Sep 23 '21 at 06:08
  • @ysdx: yes, `typeid` seems to be designed for polymorphic objects (https://en.cppreference.com/w/cpp/language/typeid) – Steven Lee Sep 23 '21 at 06:23
13

You can't do that from within a constructor (or destructor) - neither with typeid nor with a virtual method. The reason is while you're in a constructor the vtable pointer is set to the base class being constructed, so the object is of base class and no amount of polymorphism will help at that point.

You have to execute that code after the most derived class has been constructed. One option would be to use a factory function:

template<class T>
T* CreateInstance()
{
    T* object = new T();
    cout << typeid(*object).name() << endl;
    return object;
}
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Of course, the vtable pointer setting is just a behind-the-scenes implemention of the C++ rule: An object under construction has the type of the ctor that's running. – MSalters Jul 19 '11 at 12:34
0

Another option is to provide a virtual toName() function

struct Object{
  virtual std::string toName() const = 0;
}
struct Base: Object{
  std::string toName()const{ return "Base"; }
}
struct Derived: Base, Object{
  std::string toName()const{ return "Derived"; }

This might get tedious since you need to manually create each toName function. But the advantage it gives you is to provide your own custom name.

dchhetri
  • 6,926
  • 4
  • 43
  • 56