steveo225
steveo225

Reputation: 11882

Programmatically getting the name of a derived class

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.

Upvotes: 24

Views: 23481

Answers (4)

c z
c z

Reputation: 8958

Since the type is not polymorphic, the base class will never see the derived class, irrespective of any initialization order taking place in the constructor, it simply hasn't got a vtable with which to access it.

You cannot get around the virtual method call, all typeid is doing on a dynamic type is following the vtable. Use compile-time templates if you want to avoid this.

We can exemplify this by outputting in a method instead of the constructor. Contrast the following code with and without virtual.

#include <iostream>

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

    void Foo() {
        std::cout << "In method: " << typeid(*this).name() << std::endl;
    }

    /*virtual*/ ~Base() = default;
};

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

int main() {
    Base b;
    Derived d;
    MoreDerived md;
    
    b.Foo();
    d.Foo();
    md.Foo();
}

When not polymorphic:

In ctor: Base
In ctor: Base
In ctor: Base
In method: Base
In method: Base
In method: Base

Polymorphic:

In ctor: Base
In ctor: Base
In ctor: Base
In method: Base
In method: Derived
In method: MoreDerived

Upvotes: 1

ysdx
ysdx

Reputation: 9315

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 :

Upvotes: 17

dchhetri
dchhetri

Reputation: 7136

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.

Upvotes: 1

sharptooth
sharptooth

Reputation: 170479

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;
}

Upvotes: 15

Related Questions