Go Down

Topic: C++ pointer to template polymorphism (Read 521 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.

Nick Gammon


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.
http://www.gammon.com.au/electronics

obiwanjacobi

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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview