Francesco S. Carta wrote:
Francesco S. Carta <entuland@gmail.com>, on 12/07/2010 22:29:59,
wrote:
Rui Maciel <rui.maciel@gmail.com>, on 12/07/2010 21:08:03, wrote:
I have a class which has a set of friend functions. Now I wish to
convert that class to a template
class but I'm having trouble declaring the friend functions of
that template class. Can anyone
help? Any tip is more than welcomed.
It's a bit tricky, but possible:
//-------
template<class T> class foo {
public:
foo(T t): t(t) {}
protected:
T t;
template<class U> friend void inspect_foo(foo<U> f);
};
template<class T> void inspect_foo(foo<T> f) {
cout << f.t << endl;
}
//-------
Well, no, the word I meant to use wasn't exactly "tricky" but more
on the side of "verbose / tedious" - if you have lots of overloads
it's way easier to put them in a class and make that a friend, just
like suggested by Victor.
I wonder what's the rationale of not allowing to simply say:
friend function_name;
and set all overloads of "function_name" as friends, regardless of
their arguments or return types - I suppose the mechanism is not as
simple as I'd expect it to be, with these words of mine.
access to the private class members. Could as well make them public.