473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Friendship and inheritance

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
3 4011
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
/// 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
2
by: hanna88 | last post by:
hello... i am looking for a better way of doing Class Matrix. the current code i have now is Class Vector which is inherited from Class Base. Class Matrix needs the vector in Class Vector and...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.