public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to use friend functions.
@ 2004-03-02  0:28 Jeffrey Holle
  2004-03-02  2:56 ` Jeffrey Holle
  2004-03-02 13:48 ` Eljay Love-Jensen
  0 siblings, 2 replies; 4+ messages in thread
From: Jeffrey Holle @ 2004-03-02  0:28 UTC (permalink / raw)
  To: gcc-help

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?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use friend functions.
  2004-03-02  0:28 How to use friend functions Jeffrey Holle
@ 2004-03-02  2:56 ` Jeffrey Holle
  2004-03-02 11:17   ` Claudio Bley
  2004-03-02 13:48 ` Eljay Love-Jensen
  1 sibling, 1 reply; 4+ messages in thread
From: Jeffrey Holle @ 2004-03-02  2:56 UTC (permalink / raw)
  To: gcc-help

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?
> 
> 
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use friend functions.
  2004-03-02  2:56 ` Jeffrey Holle
@ 2004-03-02 11:17   ` Claudio Bley
  0 siblings, 0 replies; 4+ messages in thread
From: Claudio Bley @ 2004-03-02 11:17 UTC (permalink / raw)
  To: gcc-help

On Mon, Mar 01, 2004 at 06:53:47PM -0800, Jeffrey Holle wrote:
> 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?

Please don't try to paraphrase, just post example code which exhibits the
error plus the command line incl. options plus the errors GCC produced. It's
way easier to provide help this way. 

On to the problem. Try this:

template <class T> void my_fun (const string&, T&);

class A {
  friend void my_fun<> (const string&, A&);
  void call_me () { };
};

template <class T> 
void my_fun (const string& str, T& obj)
{
  obj.call_me ();
}
  
Note, however, this will only work if the function is declared in the
nearest enclosing namespace scope of class A.

PS: Don't top-post. TIA. 
-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux user                         - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use friend functions.
  2004-03-02  0:28 How to use friend functions Jeffrey Holle
  2004-03-02  2:56 ` Jeffrey Holle
@ 2004-03-02 13:48 ` Eljay Love-Jensen
  1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-03-02 13:48 UTC (permalink / raw)
  To: jeff.holle, gcc-help

Hi Jeff,

Could you provide a 10-20 line (nearly) compilable illustrative example of 
what you are trying to attempt.

I'm using GCC 3.3.1 and my attempt to recreate your issue failed, since the 
friend function worked fine in my toy program.

In the absence of such an example, I suspect that your problem is that you 
are not forward declaring your function before declaring it a friend in 
your class.

Thanks,
--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-03-02 13:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-02  0:28 How to use friend functions Jeffrey Holle
2004-03-02  2:56 ` Jeffrey Holle
2004-03-02 11:17   ` Claudio Bley
2004-03-02 13:48 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).