Go Down

Topic: C++ pointer to template polymorphism (Read 532 times) previous topic - next topic

obiwanjacobi

Hi

I have a template class where I need to get a 'generic' pointer to (in order to call a method on that template class).

Code: [Select]
template<class BaseT>
class TemplateClass : public BaseT
{
};

// does not compile
TemplateClass<>* pObj = NULL;


I do not want to use a base class (type polymorphism) because it's part of a library and I do not want to dictate class hierarchies.
Most classes in this lib use to BaseT template parameter to derive from.

Is there any way to do this or should I search for an different solution?

Thanx,
Marc

Coding Badly

Quote
I do not want to use a base class (type polymorphism) because it's part of a library and I do not want to dictate class hierarchies.


Why are the two mutually exclusive?  Can't you have a simple base class with the (abstract) method of interest and use templates roughly the way you are now?

pYro_65

This is the limitation of static polymorphism, you can only get around this using dynamic methods... as you want the template paramater of TemplateClass to be dynamic.

CRTP is true polymorphism, just static.


I have a template class where I need to get a 'generic' pointer to (in order to call a method on that template class).

I do not want to use a base class (type polymorphism) because it's part of a library and I do not want to dictate class hierarchies.
Most classes in this lib use to BaseT template parameter to derive from.


I don't see how that can work. You need to call a virtual method, surely? And that will be in the base class. Put it another way, what method? Give a more concrete example.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

obiwanjacobi

Thank you. I will look for another way...

Go Up