473,398 Members | 2,368 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,398 software developers and data experts.

Inner classes and friend

I have a graph data structure. It goes something like this:

class GraphNode {
private:
friend class Graph;
// successor GraphNodes stored as indices of the Graph's array
int value;
int next1, next2;
};

class Graph {
private:
std::vector<GraphNodenodes;
class cmp_val {
public:
operator()(const GraphNode &l, const GraphNode &r) {
return (l.value r.value) - (l.value < r.value);
}
};
int find_node(int val);
};

Here, Graph::cmp_val is a function object used by Graph::find_node in order
to find a node with a given value in the nodes vector. However, although
Graph is a friend of GraphNode, Graph::cmp_val doesn't inherit this
friendship, so its operator()() function cannot access l.value and r.value.

Why don't inner classes inherit their outer classes' friendships?

Is there a good reason I shouldn't just add "friend Graph::cmp_val;" to
GraphNode as well? It seems somehow wrong to me, but I can't say why.

--
Philip Potter

Aug 14 '06 #1
6 5532
"Philip Potter" <ph***********@xilinx.comwrote in message
news:eb*********@cliff.xsj.xilinx.com
I have a graph data structure. It goes something like this:

class GraphNode {
private:
friend class Graph;
// successor GraphNodes stored as indices of the Graph's array
int value;
int next1, next2;
};

class Graph {
private:
std::vector<GraphNodenodes;
class cmp_val {
public:
operator()(const GraphNode &l, const GraphNode &r) {
return (l.value r.value) - (l.value < r.value);
}
};
int find_node(int val);
};

Here, Graph::cmp_val is a function object used by Graph::find_node in
order to find a node with a given value in the nodes vector. However,
although Graph is a friend of GraphNode, Graph::cmp_val doesn't
inherit this friendship, so its operator()() function cannot access
l.value and r.value.

Why don't inner classes inherit their outer classes' friendships?
Probably the same reason derived classes don't inherit their base classes'
friendship. Good design says that friendship should be minimised, so you
have to make it explicit.
Is there a good reason I shouldn't just add "friend Graph::cmp_val;"
to GraphNode as well? It seems somehow wrong to me, but I can't say
why.
With very simple classes, it probably makes no difference either way. You
could define a member function of Graph that cmp_val calls to give cmp_val
the access it needs --- or you could do the same in GraphNode.

--
John Carson
Aug 14 '06 #2
The statement "friend class Graph;" in class GraphNode means Graph is
the fried of GraphNode, that it can access the private data in Graph.
To let the inner class cmp_val to access the data of GraphNode, you
should declare GraphNode as Graph's friend.
Remove the statement "friend class Graph" from class GraphNode, but
add "friend class GraphNode " in class Graph.

Philip Potter wrote:
I have a graph data structure. It goes something like this:

class GraphNode {
private:
friend class Graph;
// successor GraphNodes stored as indices of the Graph's array
int value;
int next1, next2;
};

class Graph {
private:
std::vector<GraphNodenodes;
class cmp_val {
public:
operator()(const GraphNode &l, const GraphNode &r) {
return (l.value r.value) - (l.value < r.value);
}
};
int find_node(int val);
};

Here, Graph::cmp_val is a function object used by Graph::find_node in order
to find a node with a given value in the nodes vector. However, although
Graph is a friend of GraphNode, Graph::cmp_val doesn't inherit this
friendship, so its operator()() function cannot access l.value and r.value.

Why don't inner classes inherit their outer classes' friendships?

Is there a good reason I shouldn't just add "friend Graph::cmp_val;" to
GraphNode as well? It seems somehow wrong to me, but I can't say why.

--
Philip Potter
Aug 14 '06 #3
"xiaohuamao" <ca**************@gmail.comwrote in message
news:11*********************@74g2000cwt.googlegrou ps.com...
The statement "friend class Graph;" in class GraphNode means Graph is
the fried of GraphNode, that it can access the private data in Graph.
Er... no. The reason this isn't true is that there would be no point in me
declaring my data private: because anybody could get at the guts of my class
by making mine a friend of thiers.

Philip

Aug 14 '06 #4

"John Carson" <jc****************@netspace.net.auwrote in message
news:44***********************@un-2park-reader-01.sydney.pipenetworks.com.au...
"Philip Potter" <ph***********@xilinx.comwrote in message
news:eb*********@cliff.xsj.xilinx.com
Why don't inner classes inherit their outer classes' friendships?

Probably the same reason derived classes don't inherit their base classes'
friendship.
(That's a subtly different issue. If derived classes inherited friendships,
it would break encapsulation; this isn't the case with inner classes.)
Good design says that friendship should be minimised, so you
have to make it explicit.
Yes, I can see your point here. If the GraphNode here was an inner class of
Graph, it wouldn't make sense for GraphNode to see all of Graph's mucky
internal bits, because GraphNode is just a tool to be used exclusively by
Graph.

However, it *does* make sense for function-style classes to inherit
friendship in the same way that real functions do, and to do so without
having to change the friend-ing class. It all seems a bit inelegant, as if
there's a better way of doing things. (But I suppose that whenever a class
friends another class, it implies some sort of cosy relationship between the
two, and so adding extra friend declarations for inner classes is probably
perfectly alright.)

--
Philip Potter

Aug 14 '06 #5
"Philip Potter" <ph***********@xilinx.comwrote in message
news:eb*********@cliff.xsj.xilinx.com
"John Carson" <jc****************@netspace.net.auwrote in message
news:44***********************@un-2park-reader-01.sydney.pipenetworks.com.au...
>"Philip Potter" <ph***********@xilinx.comwrote in message
news:eb*********@cliff.xsj.xilinx.com
>>Why don't inner classes inherit their outer classes' friendships?

Probably the same reason derived classes don't inherit their base
classes' friendship.

(That's a subtly different issue. If derived classes inherited
friendships, it would break encapsulation; this isn't the case with
inner classes.)
Good point. There is a difference there.
>Good design says that friendship should be minimised, so you
have to make it explicit.

Yes, I can see your point here. If the GraphNode here was an inner
class of Graph, it wouldn't make sense for GraphNode to see all of
Graph's mucky internal bits, because GraphNode is just a tool to be
used exclusively by Graph.

However, it *does* make sense for function-style classes to inherit
friendship in the same way that real functions do, and to do so
without having to change the friend-ing class.
Yes, but there is no clear division between function-style classes and other
classes. A class that defines operator() may do all sorts of other things.

--
John Carson
Aug 14 '06 #6
xiaohuamao wrote:
The statement "friend class Graph;"
Please read the information below.

Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Aug 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
3
by: Glen Able | last post by:
class Outer { typedef int MyInt; class Inner { Outer::MyInt m_thing; }; };
2
by: Carlos Martinez | last post by:
Hi all: Supose the following classes: class A { class InnerA1 { };
2
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
1
by: mrstephengross | last post by:
I'm making progress on mixing templates with friends (sounds like a drinking game, huh?). Anyway, here's the situation. I've got an "Outer" class with a private "Inner" class (sub-class,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
5
by: Daniel T. | last post by:
The goal is to make a friend function of an inner template class... template < typename T > class Foo { public: class Bar { friend bool operator==( const typename Foo<T>::Bar& lhs, const...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.