'Get type of child class at runtime without RTTI in C++ from base pointer
I have the problem that i need to check if a given type is the same as one specific one.
Simple example:
class Base {
public:
virtual void hi() = 0;
};
class A : public Base {
public:
void hi() { printf("Hi! I am A\n"); }
};
class B : public Base {
public:
void hi() { printf("Hi! I am B\n"); }
};
void isA(Base* something) {
// 1
if (something.is(A)) {
A* aSomething = (A*)something;
aSomething->hi();
}
// 2
something->hi();
}
Output:
Hi! I am A
Hi! I am A
The problem is that i have no RTTI enabled (and can't enable it) so i do not have dynamic_cast or typeid(...). But the program somehow still knows which type it is, as the part after // 2 still works correctly including polymorphism.
In my exact scenareo, A is a template. So i only need to check if the given pointer to the base is of type A with the same template arguments.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
