Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialising base class reference members from a derived class

Reply
Thread Tools

Initialising base class reference members from a derived class

 
 
Tim Clacy
Guest
Posts: n/a
 
      03-03-2004
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


 
Reply With Quote
 
 
 
 
Tim Clacy
Guest
Posts: n/a
 
      03-03-2004
....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?


 
Reply With Quote
 
 
 
 
Tim Clacy
Guest
Posts: n/a
 
      03-03-2004
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).


 
Reply With Quote
 
Nick Hounsome
Guest
Posts: n/a
 
      03-04-2004

"Tim Clacy" <(E-Mail Removed)> wrote in message
news:40460d4a$0$236$(E-Mail Removed)...
> 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
>
>



 
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
Accessing derived class members from base class Bhawna C++ 7 08-26-2008 11:03 AM
Initialising private base members in copy constructor Fraser Ross C++ 7 08-07-2007 07:55 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments