How to do the implementation?

Discussion in 'C++' started by foggy, Apr 23, 2004.

  1. foggy

    foggy Guest

    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!
    foggy, Apr 23, 2004
    #1
    1. Advertising

  2. "foggy" <> wrote in message
    news:N83ic.25491$...
    > 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
    Ivan Vecerina, Apr 23, 2004
    #2
    1. Advertising

  3. "foggy" <> wrote in message news:<N83ic.25491$>...
    > 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
    Michiel Salters, Apr 23, 2004
    #3
  4. foggy

    Paul Guest

    > 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" <> wrote in message
    news:N83ic.25491$...
    > 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!
    >
    >
    Paul, Apr 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VBDotNETCoder

    Need MSF Implementation Plan

    VBDotNETCoder, Nov 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    703
    richlm
    Nov 18, 2004
  2. Anand P Paralkar

    UART Implementation

    Anand P Paralkar, Jun 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    12,927
    Rudolf Usselmann
    Jul 7, 2003
  3. Michael Tsang
    Replies:
    32
    Views:
    1,107
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,192
    Phil Carmody
    Mar 30, 2010
  5. sanket
    Replies:
    7
    Views:
    1,002
    Tsung
    Nov 3, 2011
Loading...

Share This Page