template <class T> void checkObject(T genericObject)
{
MyClassA* a = dynamic_cast<MyClassA*>(genericObject);
if (a != NULL)
{
//we know it is of type MyClassA
}
MyClassB* b = dynamic_cast<MyClassB*>(genericObject);
if (b != NULL)
{
//we know it is of type MyClassB
}
}
Is something like this possible? where we have a template type but we want to know it's actual type?
In the world of templates you probably want to just specialize templates for each of your types instead of doing a runtime check, ie
template<typename T>
void foo(T obj);
template<>
void foo<MyClassA>(MyClassA obj) {
}
template<>
void foo<MyClassB>(MyClassB obj2) {
}
This will allow the compiler to generate the correct template at compile time by deducing on your args.
Note this only resolves based on a instance's static type, that is there's no compile-time knowledge that your variable is a MyClassC
which inherits from MyClassB
and therefore should use the generic form. So this won't work:
MyClassC* cinstance = new MyClassC();
foo(cinstance); //compiler error, no specialization for MyClassC
In general this points to a general rule that compile-time and run-time polymorphism are very different systems. Templates deal strictly in the realm of static types without knowledge of inheritance. This may surprise folks coming from Java/C# which have a more seamless integration between the two features.
For run-time specialization of functionality for a class, your options are
It is possible but MyClassA
and MyClassB
must have at least one virtual member function in order for dynamic_cast
to work. I also believe you actually want to have (T* genericObject
) rather than T genericObject
in your function's signature (it would make little sense otherwise).
Solutions based on template specializations are OK for static polymorphism, but I believe the question is how to enable run-time detection of the input's type. I imagine that template being called with a pointer which is of a type that is either a superclass of MyClassA
or a superclass of MyClassB
. Template specialization would fail to provide the right answer in this case.
Anyway, I have a strong feeling that you are trying to do the wrong thing to achieve what you want to achieve (whatever it is). When you post this kind of questions, I suggest you to make clear where you want to go, what is your goal; this one might just be an obstacle along the wrong path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With