Initialising base class reference members from a derived class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Clacy

    Initialising base class reference members from a derived class

    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

    #2
    Re: Initialising base class reference members from a derived class

    ....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?


    Comment

    • Tim Clacy

      #3
      Re: Initialising base class reference members from a derived class

      Tim Clacy wrote:[color=blue]
      > ...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?[/color]

      ....ignore that (red herring).


      Comment

      • Nick Hounsome

        #4
        Re: Initialising base class reference members from a derived class


        "Tim Clacy" <nospamtcl@nosp amphaseone.nosp amdk> wrote in message
        news:40460d4a$0 $236$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
        > Can this be done... or is there a better way to achieve the same[/color]
        objective?[color=blue]
        >
        >
        > 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[/color]
        of[color=blue]
        > references... but why necessary; surely the compiler has all the[/color]
        information[color=blue]
        > 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;
        > :
        > };
        >[/color]

        What makes you think that adding a constructor will make any difference?
        Interface::Inte rface(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.

        [color=blue]
        >
        >
        > struct Implementation : Interface
        > {
        > Implementation( ) :
        > I1 (Connect(fn1)),
        > I2 (Connect(fn2)))
        > { }
        >
        > void fn1();
        > void fn2();
        > :
        > };
        >
        >
        >
        > Eager to see what the gurus say...
        >
        >
        > Tim
        >
        >[/color]


        Comment

        Working...