By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Friendship and inheritance

P: n/a
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

Sep 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 29 Sep 2006 00:45:24 -0700, to*********@gmail.com wrote:
>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.
>
(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.
>
Thanks very much,

Tom
You are welcome,

Zara
Sep 29 '06 #2

P: n/a
/// snippage

Zara wrote:
On 29 Sep 2006 00:45:24 -0700, to*********@gmail.com wrote:
(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.
I've now looked into both these things and I'm afraid I don't
understand
how either of them applies here: could you give a bit more detail? I'd
be very grateful.

Cheers,

Tom

Sep 29 '06 #3

P: n/a
On 29 Sep 2006 05:32:39 -0700, to*********@gmail.com wrote:
>/// snippage

Zara wrote:
>On 29 Sep 2006 00:45:24 -0700, to*********@gmail.com wrote:
>(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.

I've now looked into both these things and I'm afraid I don't
understand
how either of them applies here: could you give a bit more detail? I'd
be very grateful.
Anything you want to access from a friend function must be in the
original class, otherwise the function should not compile.

class Base {
friend int whatever(Base& object);
private:
int myInternalData;
public:
int increment() {return ++myInternalData;}
};

class Derived:public Base {
private:
public:
int incremenByTwo() {increment();return increment();}
};

int whatever(Base &object) {
object.incrementByTwo();
}

int main() {
Derived d;
return whatever(d);
}

WRONG: Base has no memeber function called incrementByTwo();

How to access functions from derived classes? You use virtual
functions:

class Base {
friend int whatever(Base& object);
private:
int myInternalData;
public:
virtual int increment() {return ++myInternalData;}
};

class Derived:public Base {
private:
public:
virtual int increment() {
Base::increment();
return Base::increment();
}
};

int whatever(Base &object) {
object.increment();
}

int main() {
Derived d;
return whatever(d);
}

RIGHT: d will be increment twice

As for template pattern, let's see another way to do that. It is
horrible, it is not a good example of Template, but it might be useful
to see the idea:

class Base {
friend int whatever(Base& object);
private:
int myInternalData;
virtual int getIncrementStep() {return 1};
public:
int increment() {
return myInternalData+=getIncrementStep();
}
};

class Derived:public Base {
private:
virtual int getIncrementStep() {return 2};
public:
};

int whatever(Base &object) {
object.increment();
}

int main() {
Derived d;
return whatever(d);
}

RIGHT: And the common part of the algorithm increment(), that is, to
add something to the internal data is common to Base and all Derved
classes there might be. The only part that varies from class to class
is the setting of the increment step

Hope this helps you.

Zara

Sep 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.