Click to See Complete Forum and Search --> : friends in class templates


BVK Chaitanya
April 23rd, 2002, 01:24 AM
hello,
i have two template functions declared like below.

//////////////////

template <class A, class B>
void f1 (void);

template <class A, class B>
void f2 (void);

/////////////////////

and i have a class template as below


//////////////////////////

template <class T>
class test {
//.....
public:

// two friend declarations required here for f1 & f2;
};


/////////////////////////


i want the friend declarations in such a way that f1<XXX,YYY> can access private members of test<YYY> only, it can't have access to test<XXX> class;

similarly t2<AAAA,BBBB> should have acces to test<AAAA> only.


You got my question? how should i achieve this? how should be the friend declarations for f1, f2 functions in class test?

Please I really want this. Please give me a detailed explanation.

Thank you very much.

-Chaitu

NMTop40
April 23rd, 2002, 07:23 AM
I'm not sure that it can be done, and certainly not all compilers support it.

It is quite possible that what you are doing can (and should) be done in another way. After all, the template function would have to know enough detail of your class to know what private features it wants to access, and the coupling is therefore fairly tight here. Perhaps, therefore, t1 and t2 should both therefore be template member functions of the classes that they are supposed to have the access to. Otherwise, is there no way they can do what they are trying to achieve with public members?

I'd have to see your exact example to help determine how else you could achieve that which you are trying to do.


The best things come to those who rate