Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to do the implementation?

Reply
Thread Tools

How to do the implementation?

 
 
foggy
Guest
Posts: n/a
 
      04-23-2004
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!


 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      04-23-2004
"foggy" <(E-Mail Removed)> wrote in message
news:N83ic.25491$(E-Mail Removed)...
> 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


 
Reply With Quote
 
 
 
 
Michiel Salters
Guest
Posts: n/a
 
      04-23-2004
"foggy" <(E-Mail Removed)> wrote in message news:<N83ic.25491$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      04-23-2004
> 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" <(E-Mail Removed)> wrote in message
news:N83ic.25491$(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments