lundi 7 décembre 2015

C++ alternate class implementation by rename class with its constructor

According to this question, I can't use my typedef-ed typename into constructor.

In my current project, I have to implement a class in two different ways, which can be altered by simply add single macro statement. The code looks like this

#ifndef USE_BAR
class Bar; //declare class Bar in Bar.h and implement it in Bar.cpp
typedef Bar Foo;
#elseif /* USE_BAR */
class Foo; //declare class Foo in Foo.h and implement it in Foo.cpp
#endif /* USE_BAR */

Bar and Foo have exactly same methods, and same constructor overload lists. However with this code, I can't use Foo* fooInstance = new Foo(); line so far as USE_BAR declared because the constructors of Bar won't be renamed to Foo. I'm struggling because I want to use a class named Foo only, not Bar, outside declaration/implementation of these classes, while I also want to distinguish this Foo and Bar implementation by naming them differently.

Is there any code design pattern to achieve both?

Aucun commentaire:

Enregistrer un commentaire