I have a few friend function to a class which i want to place in the same header files.
Regarding the implementation and interface details, i don't want it to be visible to people. Is there any way of hiding these friend functions commpletely. ??
You can put them in a nested namespace, they will be visible but not too much.
Why do you want to put them in the header? can't you hide them in some source file?
I could. But i am using a templated version of class. So i can't put that ( well i can, but that gave me bugs last time) in .cpp file (not even the implementation). So I have divided the interface and implementation in two different header files. Now creating a .cpp for just friends doesn't seem right.
but at the same time i don't want people to start using my friend functions (which is defined outside the class ) .
put your friend functions in a private class and make your template friends:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class internal
{
private:
staticvoid AFriendFunc();
template <typename T> friendclass MyTemplate<T>;
// * double check this friend line, I'm not sure I did it right *
};
template <typename T>
class MyTemplate
{
//...
friendclass internal;
//...
};
Now only your template class can access the 'internal' members
class allFriendFns
{
private:
//all friend functions to the class BSTree
template<class T> staticbool lookUp(BSTNode<T>*, T);
template<class T> static BSTNode<T>* insertWith (BSTNode<T>*, T);
template<class T> staticvoid printNodeVals(BSTNode<T>*);
template<class T> staticvoid friendSize(BSTNode<T>*);
//adding friend fn becomes important because of recursions involved
template<class T> friendclass BSTree;
};
template<class T> void friendSize(BSTNode<T>* thisNode)
{
if (thisNode == NULL)
return;
else
{
BSTree<T>::treeSize++;
friendSize(thisNode->left);
friendSize(thisNode->right);
}
}
This gives me an error saying that treeSize is a private member of BSTree.
I am sure we can access private variables from friend functions. Why is this giving that error then ???
Hi,
so the problem i am facing is , making these functions static prevents me from using the private variables of the friend class. hence it becomes impossible for me to change the values of these variables. So is there a way of creating a friend class containing functions without declaring them static and using them. or i will have to do away with the whole "class for friends" thing?