Pages: [1]   Go Down
Author Topic: C++ pointer to template polymorphism  (Read 495 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 1
Posts: 93
Profile before you Optimize.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Netherlands
Offline Offline
Jr. Member
**
Karma: 1
Posts: 93
Profile before you Optimize.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: