namespaces and spacing the friend funcs

Hi All,

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:
  static void AFriendFunc();

  template <typename T> friend class MyTemplate<T>;
  // * double check this friend line, I'm not sure I did it right  *
};

template <typename T>
class MyTemplate
{
//...
  friend class internal;
//...
};


Now only your template class can access the 'internal' members
AWESOME!! :)

Thx!
navderm

is it important to call myTemplate<T> a friend of internal ???

Yes, otherwise your template classes won't be able to access anything in internal.
1
2
3
4
5
6
7
8
9
template<class C> class BSTree
{
    private:
    //public:
        BSTNode<C>* rootPtr;
        int treeSize;
    private:
        friend class allFriendFns;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class allFriendFns
{
    private:
        //all friend functions to the class BSTree
        template<class T> static bool lookUp(BSTNode<T>*, T);
        template<class T> static BSTNode<T>* insertWith (BSTNode<T>*, T);
        template<class T> static void printNodeVals(BSTNode<T>*);
        template<class T> static void friendSize(BSTNode<T>*);
        //adding friend fn becomes important because of recursions involved
        template<class T> friend class 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 ???
Last edited on
1
2
template<class T> 
void allFriendFns::friendSize(BSTNode<T>* thisNode)


You forgot to mark it as a member of allFriendFns. PS: Doesn't allFriendFns also need to be declared as a template for this to work?
Last edited on
But i have declared the whole class as its friend. why would i need to declare one more variable ??

void friendSize() is a global function, and
void allFriendFns::friendSize() a member function... you do know how to deal with classes, don't you?
i don't even know what to comment on such a f***en blunder ! sorry!
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?
Topic archived. No new replies allowed.