Alternative to virtual template function
I've recently begun a project in C++ after being immersed in C# for a
long time, and now I'm stumped on a problem I'm trying to solve. My
main issue is that I'm trying to use templates in the same fashion that
I'd be using generics in C# or Java. Here's what I'm attempting:
Because my program is going to be cross-platform, I'm writing an
Application class that interfaces with an abstract IPlatform object
pointer (a facade). My intent is to keep the Application class code
the same among all the platforms and write multiple concrete classes
within each platform-specific project that implement the facade's
interface. Things were going smoothly using this approach until I came
across the need for generics.
Since some of my target platforms don't provide an STL implementation,
I decided to create interfaces mimicking nice modern container classes
as well as factory methods in IPlatform that create the objects. For
example (pardon my simplified examples):
template<class T> class IList
{
public:
virtual int getCount() = 0;
// etc
}
class IPlatform
{
public:
template<class T> virtual IList<T>* createList() = 0;
// etc
};
However, this is not allowed, since template members cannot be virtual!
So I ditched the member function idea and went the route of
regular-old functions instead:
In a platform-independent header:
template<class T> IList<T>* createList();
In a platform-specific header:
template<class T> StlListAdapter : public IList<T>
{
public:
// blah, blah
};
// Placed in a header, since you apparently can't put
// implementation in a CPP file >:(
template<class T> IList<T>* createList()
{
return new StlListAdapter<T>();
}
Unfortunately, this is causing me linking errors when I try to call the
function defined in the platform-independent header. Argh! So now I'm
left with the sinking feeling that I'm trying to do something that
really cannot be done very elegantly using C++.
Am I making some coding mistakes, or is this really something that C++
is not good at? Do any of you have an alternative approach that you
would take to solve this problem?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]