How to do the implementation?

F

foggy

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!
 
I

Ivan Vecerina

foggy said:
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!
The question is quite abstract and imprecise. This said:

Instead of a single subclass of IFace, you should
probably have 3 subclasses that contain either C, D or E.
The right subclass will be instantiated/accessed based
on the value of switcher.


hth, Ivan
 
M

Michiel Salters

foggy said:
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!

class B : public IFace
{
enum impl_variants { typeC = 0, typeD=-1, typeE=1 };
const impl_variants m_type;
union {
C* c;
D* d;
E* e;
} ptr;
public:
B::B( int switcher ) :
m_type( static_cast<impl_variants>( switcher ) )
{
switch( m_type )
{
case typeC: ptr.c = new C; break;
....

Regards,
Michiel Salters
 
P

Paul

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top