This is the scenario:
class Base {
public:
typedef Base type;
};
class Derived: public Base {
public:
typedef Derived type;
};
I want something like:
int main() {
Base * bs = new Derived();
decltype(*bs)::type newvar;
}
How can i do something like above to get the type of the Derived class without using static / dynamic cast ?
No you cannot do that.
C++ is statically typed language, which means a type has to be determined at compile time.
In the example
bsdoesn’t know what object type it’s pointing.See a scenario below to make it clearer:
You have to rely on
virtualmechanism ordynamic_castfor achieving your ultimate purpose.