Class A is a container for Class B instances.
File A.h:
#include "B.h"
Class A
{ public:
unique_ptr<B> getInstanceOfB();
private:
unique_ptr<map<int, shared_ptr<B>>> uptr_BInstancesMap;
map<string, int> map__BUniqueValues;
friend int B::getValueIndex( const string );
};
File B.h:
Class B
{ public:
unique_ptr<B> returnInstanceOfB();
// int getValueIndex( const string ); // Compiles fine
private:
int int__ValueIndex;
int getValueIndex( const string ); // "error: 'int B::getValueIndex(std::string)' is private"
}
There may be upwards of 100,000 instances of B stored in the instance of A, but there will be far fewer unique values represented by those instances of B. So the idea is to store the unique values in the instance of A and store the index to its (shared) value in each instance of B. Seems like a reasonable scenario for a friend function.
getValueIndex() is for use within Class B however; it shouldn't be public. Would someone explain (perhaps with a dangerous example) why the friend function definition can't be private?
There is a "circularity" here.
friend in A means that the declared function can access A private parts.
But to access that declaration, it has to be accessible to A itself, hence B must have A as a friend.
I understand your wish of "purity", but C++ is all but an "pure" language. If A and B are closely related and you are responsible for both of them, instead of go crazy with single declarations, just make them each other friends.
No one -outside of them- can do anything dangerous, and you are already in you own "danger zone", you already know about... so why the complication?