Re: How to restrict 'some' code from getting compiled/built? (without
#define)
On Jun 6, 11:37 pm, "Sachin Garg" <saching...@c10n.info> wrote:
I need to build two executables from my code, one having all
the code (and thus application features) and other not having
it all. How to best manage the code that shouldn't go in one
of the executables?
My first thought is to use conditional compilation with
#define and #ifdef etc but its getting messy, are there better
ways to manage this?
The problem in detail:
#. There is a class A with virtual foo1 and foo2
#. There are 'lots' of derived classes of A which implement foo1 and foo2
#. Executable1 needs to use both foo1 and foo2
#. Executable2 only needs to use foo1, never needs foo2
How to make sure that all the foo2 code never gets into
executable2? Lots of #ifdefs can be used for this but is there
a better solution? Maybe some clever use of templates?
Something else less complicated?
Generally, if a class has two different interfaces, it's two
different classes. You'd have to be more concrete; the usual
solution for conditionally including functions and/or objects is
to put the conditional objects in separate source files, and
either link the corresponding object files in or not.
In your case, it looks to me like you have two separate
hierachies: one with just foo1, and one with both. This could
be done using multiple inheritance, something like:
class Base
{
public:
virtual void foo1() = 0 ;
} ;
class Derived : public virtual Base
{
public:
virtual void foo1() ;
} ;
and:
class BaseOptional : public virual Base
{
public:
virtual void foo2() = 0 ;
} ;
class DerivedOptional : public virtual Derived
{
public:
virtual void foo2() ;
} ;
Put the ...Optional in a separate directory and library.
Personally, however, I'd reconsider the design before I adopted
such complexity. Couldn't two separate hierarchies be made to
work as well, if not better?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34