473,321 Members | 1,708 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,321 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 5529
"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.