How to use friend functions.

Jeffrey Holle jeff.holle@verizon.net
Tue Mar 2 02:56:00 GMT 2004


More info:
I have successfully used my templated function when I do not make the 
methods that it uses private.  This avoids the friend issues that I'm 
having problems with.

I do not want to keep my app this way.  There are specific reasons I 
want to restrict access to these methods, so I'd really like to know why 
friend functions don't work.

Jeffrey Holle wrote:
> I use gcc v3.3.
> I'm developing an application in which I want to restrict access to a 
> member function to everything but 1 function.
> 
> I'm attempting to accomplish this by making the method private and my 
> function a friend, but failing.
> 
> The synopsis of the function is:
>   template<class T1> void putNamespace(const string& name, T1& object);
> 
> I attempt to define this function has a friend of CParameter with the 
> following in the public definition area of this class.
>   friend void putNamespace(const string& name, CParameter& object);
> 
> I've tried to indicate what this function actually is (a template), but 
> run into compiler errors.
> 
> It compiled the shared library that I'm defining it in, but when I 
> attempt to compile a client that uses it like:
>     putNamespace("std",myParameter);
> I get errors associated with putNamespace attempting to a private method 
> of CParameter, which it does!
> 
> I've backed off from the template as an experiment.  In defining 
> putNamespace to take a second parameter of "CParameter", I get these 
> compilation errors in compiling my shared library.
> 
> Can anybody tell me what I'm doing wrong?
> 
> 
> 




More information about the Gcc-help mailing list