Without RTTI and with virtual functions.
I encountered 2 different common solutions to provide type of the object:
with virtual method call and keep id inside the method:
class Base {
public:
virtual ~Base();
virtual int getType() const =0;
};
class Derived : public Base {
public:
virtual int getType() const { return DerivedID; }
};
with inline method call and keep id in the base class:
class Base {
int id_;
public:
virtual ~Base();
inline int getType() const { return id_; }
};
class Derived : public Base {
public:
Derived() { id_=DerivedID;}
};
What would be better choice in general and what is pro/cons of them?