Re: Choose between class implementations at compiletime
* AnonMail2005@gmail.com:
On Sep 11, 8:46 am, Christoph Mathys <cmat...@gmail.com> wrote:
Can you define the concrete class (e.g. Foo) in a header file but
implement N implementations of Foo in various .cpp files that
correspond to your APIs and put each in a separate library?
Yes, of course! Why didn't I think of that... Thanks for the hint!
Christoph
But I must say, that can complicate your build and also your testing.
Using and Abstract Base Class and some sort of a factory that returns
the correct derived class at run time based on a configuaration
parameter might be easier overall in the long run.
That's a Java technique.
In standard C++ there are no shared libraries, hence with that solution as an
*alternative* to the linking, all the code for the different systems would have
to be present, which is just not practical.
With a shared library the Java factory technique becomes practically possible,
but then it's most likely, depending on the app, just a redundant layer of
complication, since the basic shared library dynamic linking does it all.
In short, transferring Java patterns uncritically to C++ isn't a good idea.
They're two different languages, and in particular in this case, with ordinary
C++ you build a different executable for each platform (link time selection is a
good idea), while with Java you may build a common executable for all platforms
(run time selection, implying a factory, is a good idea).
Cheers & hth.,
- Alf
PS: Assuming I got your identity right, I think that may be one thing your
programmers reacted to, namely needless and quite unnatural Java-ism. ;-)