Initialising base class reference members from a derived class

T

Tim Clacy

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
 
T

Tim Clacy

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

Tim Clacy

Tim said:
...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).
 
N

Nick Hounsome

Tim Clacy said:
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.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top