Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friend declared function call

Thread Tools

friend declared function call

Markus Moll
Posts: n/a

Sven Köhler wrote:

> namespace wurst {
> class bla {
> friend bool foobla(const bla& v, int x) {
> return true;
> }
> };
> }

Defining a friend function inside a class is a bit weird. However, it's
allowed. The difference to defining it outside the class (as far as I can
see) is that it is in the scope of the class, it is not visible outside the
class and it is implicitly inline.

This means that you have to declare it in the surrounding namespace if you
want to use it there, and you don't have to qualify class members inside
the definition.

> How do i access foobla in a more specific way? What the "full qualified
> name" of foobla?

It is <namespace>::f, but it is not visible.

> The following alternatives DON'T work:
> wurst::foobla(foo,2)

Would be correct if you had previously declared wurst::foobla:

namespace wurst // the wurst possible name
bool foobla(const bla& v, int x);

> wurst::bla::foobla(foo, 2);

Definitely not.


Reply With Quote
Posts: n/a
On Apr 11, 12:07 am, Ian Collins <(E-Mail Removed)> wrote:
> Christopher Pisz wrote:
> > "Ian Collins" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...
> >> Christopher wrote:
> >>> Someone got confused there.
> >>> A friend function shouldn't be implemented in the class it is a friend
> >>> of. Otherwise, it wouldn't be a friend, but a method belonging to the
> >>> class instead.

> >> That simply isn't true. A friend has to be declared and may also be
> >> defined within the class definition.

> >> Two classic examples are I/O operators and callback functions which
> >> require extern C linkage.

> > Show example code please. I've searched my I/O libraries and cannot find the
> > keyword friend anywhere, so you must be talking of custom operators, and
> > then I still cannot fathom why the function wouldn't be a plain old class
> > method.

> The output and input operators << and >> are often declared as free
> functions, which may be friends of the class.
> --
> Ian Collins.

I've never seen those implemented _inside_ the class, but defined and
implemented outside the class and then made a friend of the class.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function declared before call but defined after - compilation fails C++ 11 05-18-2007 05:13 PM
declared or not declared ? JohnZing ASP .Net 3 02-05-2006 08:38 PM