Re: Alternative to virtual template function
jacob.h.page@gmail.com wrote:
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).
[snip]
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.
Note that STL is a red herring here. Even if you did have STL, that
would merely provide a different definition of a generic container.
But you'd still face the same problem of returning parameterized type
from a factory method.
class IPlatform
{
public:
template<class T> virtual IList<T>* createList() = 0;
// etc
};
I think I understand why you didn't templatize IPlatform: your
platforms are probably things like Linux and PalmOS. Linux<double>
makes no sense.
What I don't understand is why you made IPlatform in charge of creating
generic lists. Is the act of list creation so different on each
platform that it cannot be factored into a universal interface? Look
at it this way: std::list is available on many, many platforms without
needing a platform-specific facade.
One thing you could try is make createList() a top-level function
parameterized on the platform class:
template<typename Platform> createList();
You could even specialize it for any given platform like so:
template<> createList<PalmOS>() { /* Implementation specific to PalmOS
*/ }
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]