I have an AdjacencyList class and I want all graph algorithms to be seperated from it. Let’s say all algorithms are functors and derive from GraphAlgorithm abstract base class. How do I make it work?
I am currently thinking,
class AdjacencyList
{
// ...
friend class GraphAlgorithm;
// ...
};
But is it allowing GraphAlgorithm’s sub-classes to access private members of AdjacencyList?
Any other approaches are welcome.
No, it does not. Friendship is not transitive.
Regardless, whenever you need a
frienddeclaration, you should regard it as a code smell and re-think your design.I would rather have getters in the
AdjacencyListand so aGraphAlgorithmcan operate on the members it has access to from the methods ofAdjacencyList.