> c, d, or e is created at run-time but not more than one will.
I take that to mean that switcher is not known at compile time? This is a
messy problem! We need more information to give a complete answer.
You'll have to use a union for C/D/E and store the initial value of switcher
in the class so it can be safely accessed later, unless switcher is constant
per run:
union
{
C c;
D d;
E e;
};
There is no way to conditionally embed an object except at compile time: if
C, D, or E has a constructor, you'll instead have to create it with new
(switch statement), store it in a pointer, and delete it in ~B() (which will
probably require another switch() ):
union
{
C *c;
D *d;
E *e;
};
If B does much more than hold IFace and one data member, and there is any
possibility of the data object being needed elsewhere, I'd consider using a
separate class (either an embedded member or a superclass) that holds C/D/E
and gives B orderly access to it.
It might also make sense, depending on what B is used for and how complex it
is, to make it a template by value (switcher) and provide specializations.
Then you could create a B<> object with new at runtime and it would have the
proper members.
"foggy" <fo***@turboweb.com> wrote in message
news:N8********************@bgtnsc04-news.ops.worldnet.att.net...
Given class A as the client of IFace and IFace is defined as:
int switcher;
class IFace
{
public:
virtual method1()=0;
virtual method2()=0;
};
class B : public IFace
{
// switcher is 0
C c;
// switcher is -1
(D d;)
// switcher is 1
(E e;)
};
c, d, or e is created at run-time but not more than one will.
How to do the implementation? Thanks!