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