C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Friendship and inheritance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Sep 29, 2006 9:11 am    Post subject: Friendship and inheritance Reply with quote



I know that friendship isn't inherited in C++, and have two questions.

(1) Why? Is it a technical problem, or just by design? If the latter,
isn't this a little prescriptive, and shouldn't there be a way of
explicitly inheriting friendship? (eg, class B : friendly public A)

(2) Is there a way I can simulate inheritance of friendship? I'm
working on an output library with a design based on that of iostreams:
there is a class called an OutputRegion which is fed strings through a
<< operator, processes them and displays them. I want the internal
workings of OutputRegion to be kept hidden directly from the library
user, but I do want them to be accessible by passing OutputModifiers to
<< (equivalent to iostream manipulators - eg, an OutputModifier called
NewLine which calls the OutputRegion's internal newLine function);
furthermore I want the user to be able to declare his own
OutputModifiers Here's a code snippet to illustrate what I mean:

////// begin code

class OutputModifier;

class OutputRegion
{
public:
OutputRegion& operator<< (string S)
{
/* do complicated things with S dependent on P */
return *this;
}
OutputRegion& operator<< (OutputModifier& O)
{ return O.Execute(*this); }
friend class OutputModifier;
private:
void newLine();
};

class OutputModifier
{
public:
friend class OutputRegion;
private:
virtual OutputRegion& Execute (OutputRegion& R)=0;
};

class NewLine : public OutputModifier
{
private:
OutputRegion& Execute (OutputRegion& R)
{
r.newLine(); // error here - newLine is private in this context
return R;
}
};

////// end code


(Apologies if this code doesn't compile right away - for reasons too
complicated to bother with here, my dev machine isn't connected to the
internet right now so I'm typing this from memory. I think it should be
ok barring, eg, missing semicolons).

Is there an easy way I can make this work? Or a way of simulating the
same effect, without exposing much more? Or is my design just all
wrong?


Thanks very much,

Tom
Back to top
Zara
Guest





PostPosted: Fri Sep 29, 2006 9:11 am    Post subject: Re: Friendship and inheritance Reply with quote



On 29 Sep 2006 00:45:24 -0700, tom.s.smith (AT) gmail (DOT) com wrote:

Quote:
I know that friendship isn't inherited in C++, and have two questions.

(1) Why? Is it a technical problem, or just by design? If the latter,
isn't this a little prescriptive, and shouldn't there be a way of
explicitly inheriting friendship? (eg, class B : friendly public A)

It is by design. A friend function is designed as part of the class
interfeace and, as such, it should only know about the class
implementation. Derived classes and what makes them different to teh
original "friendly" class should be completely unknown to the
function.

Quote:

(2) Is there a way I can simulate inheritance of friendship? <...

Use virtual funcitons in the friendly class. That way, the friend
function will access derived classes through an interface known to it
from the "friendly" class. And you should take a look/googgle to
"Template Design Pattern" (it does not refer to C++ templates), it is
a nice way to look at this kind of inheritance.

Quote:

Thanks very much,

Tom

You are welcome,

Zara
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.