|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi All,
I wanted to know how to declare a friend function which is a member function of a different class. Eg: /* Class which contains the member function to be declared as friend */ template <class T, int W> class A { B obj; public: void foo(T val); // Function to be declared as a friend. } template <class T, int W> void A<T,W>::foo(val) { obj.val = val; } /* Class inside which the friend function is to be declared */ template <class U> class B { U val; public: friend A<class T, class W>::foo(W val); // Declare friend. } I cant seem to get the syntax right. Please let me know. Thanks Rakesh |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
getrakesh@gmail.com schrieb:
> /* Class inside which the friend function is to be declared */ > template <class U> > class B > { > U val; > public: > friend A<class T, class W>::foo(W val); // Declare friend. > } > > I cant seem to get the syntax right. Please let me know. Something like that. template <class U> class B { U val; public: template <int W> friend void A<U, W>::foo(); // Declare friend. } But this won't work for your purpose anyway because of the cyclic dependancy of the definitions of class A and class B. A need to be defined befor B because the friend declaration won't work otherwise. B needs to be defined before A because it is a member of A. Marcel |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 26 Feb, 10:34, getrak...@gmail.com wrote:
> Hi All, > > I wanted to know how to declare a friend function which is a member > function of a different class. > > Eg: > /* Class which contains the member function to be declared as friend > */ > template <class T, int W> > class A > { > B obj; This causes a circular dependency. Also you are missing B's template parameter > public: > void foo(T val); // Function to be declared as a friend. > } > > template <class T, int W> > void A<T,W>::foo(val) > { > obj.val = val; > > } > > /* Class inside which the friend function is to be declared */ > template <class U> > class B > { > U val; > public: > friend A<class T, class W>::foo(W val); // Declare friend. Try: template<class T, int W> friend void A<T,W>::foo(W); > > } > > I cant seem to get the syntax right. Please let me know. Hope this s. DP |
|
![]() |
| Outils de la discussion | |
|
|