Initialising base class reference members from a derived class

Discussion in 'C++' started by Tim Clacy, Mar 3, 2004.

  1. Tim Clacy

    Tim Clacy Guest

    Can this be done... or is there a better way to achieve the same objective?


    If an interface class contains only references (to yet more interface
    classes), then can those references be initialised in a concrete derived
    class? To have to initialise them in the interface class constructor makes
    the interface a concrete class. I suppose they could be pointers instead of
    references... but why necessary; surely the compiler has all the information
    that it needs to do the job?

    The objective is to be able to provide a class interface to concrete
    instance(s) without exposing any implementation detail or data (private or
    otherwise); in fact, the only concrete function should be the factory
    interface. Here's the general idea [see below] where
    'Implementation::Connect' is a function that returns a reference to a
    functor derived from 'Port':


    struct Port
    {
    void virtual operator()() = 0;
    };

    struct Interface
    {
    Port& I1;
    Port& I2;
    :
    };



    struct Implementation : Interface
    {
    Implementation() :
    I1 (Connect(fn1)),
    I2 (Connect(fn2)))
    { }

    void fn1();
    void fn2();
    :
    };



    Eager to see what the gurus say...


    Tim
     
    Tim Clacy, Mar 3, 2004
    #1
    1. Advertising

  2. Tim Clacy

    Tim Clacy Guest

    ....and before I forget, is it OK to have 'virtual operator()()'? My 'virtual
    operator()()' in a derived class never gets hit... but the base class
    'virtual operator()()' does. Is this to be expected?
     
    Tim Clacy, Mar 3, 2004
    #2
    1. Advertising

  3. Tim Clacy

    Tim Clacy Guest

    Tim Clacy wrote:
    > ...and before I forget, is it OK to have 'virtual operator()()'? My
    > 'virtual operator()()' in a derived class never gets hit... but the
    > base class 'virtual operator()()' does. Is this to be expected?


    ....ignore that (red herring).
     
    Tim Clacy, Mar 3, 2004
    #3
  4. "Tim Clacy" <> wrote in message
    news:40460d4a$0$236$...
    > Can this be done... or is there a better way to achieve the same

    objective?
    >
    >
    > If an interface class contains only references (to yet more interface
    > classes), then can those references be initialised in a concrete derived
    > class? To have to initialise them in the interface class constructor makes
    > the interface a concrete class. I suppose they could be pointers instead

    of
    > references... but why necessary; surely the compiler has all the

    information
    > that it needs to do the job?
    >
    > The objective is to be able to provide a class interface to concrete
    > instance(s) without exposing any implementation detail or data (private or
    > otherwise); in fact, the only concrete function should be the factory
    > interface. Here's the general idea [see below] where
    > 'Implementation::Connect' is a function that returns a reference to a
    > functor derived from 'Port':
    >
    >
    > struct Port
    > {
    > void virtual operator()() = 0;
    > };
    >
    > struct Interface
    > {
    > Port& I1;
    > Port& I2;
    > :
    > };
    >


    What makes you think that adding a constructor will make any difference?
    Interface::Interface(Port& a,Port& b) : I1(a), I2(b) {}
    This does not expose anything that isn't already visible.

    By the way - it would be better to just use global access functions.


    >
    >
    > struct Implementation : Interface
    > {
    > Implementation() :
    > I1 (Connect(fn1)),
    > I2 (Connect(fn2)))
    > { }
    >
    > void fn1();
    > void fn2();
    > :
    > };
    >
    >
    >
    > Eager to see what the gurus say...
    >
    >
    > Tim
    >
    >
     
    Nick Hounsome, Mar 4, 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. John Harrison
    Replies:
    0
    Views:
    509
    John Harrison
    Aug 6, 2003
  2. Replies:
    1
    Views:
    422
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    408
    Victor Bazarov
    May 23, 2007
  4. Fraser Ross
    Replies:
    7
    Views:
    571
    James Kanze
    Aug 7, 2007
  5. Bhawna
    Replies:
    7
    Views:
    492
    Bhawna
    Aug 26, 2008
Loading...

Share This Page