Topic: Friend functions


Author: "David Sachs" <sachs@fnal.gov>
Date: Mon, 3 Dec 2001 04:02:29 GMT
Raw View
I have a question about friend functions, for which I could not find the
relevant information in the standard.

Does declaring a function to be a friend within a class definition declare
the function to the remainder of the program?


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Mon, 3 Dec 2001 10:07:35 GMT
Raw View
"David Sachs" <sachs@fnal.gov> wrote in message
news:9uerjp$cv0$1@info1.fnal.gov...
> I have a question about friend functions, for which I could not find the
> relevant information in the standard.
>
> Does declaring a function to be a friend within a class definition declare
> the function to the remainder of the program?

Yes and no ;-)

The function is declared to be a member of the namespace enclosing the
class, but is not found during normal name lookup unless explicitly declared
in that namespace outside the class definition. However, argument-dependent
lookup where one of the arguments causes the scope of the class being
defined to be searched will find the friend declaration.

class C
{
public:
    C(int i){}
    friend void foo(const C&);
};

int main()
{
    foo(3); // error, no foo found, even though int implicitly converts to C
    foo(C(3));  // argument dependent lookup finds the foo declared as a
friend of C.
}

The friend function foo can either be defined as part of the friend
declaration, defined later in the same translation unit, or defined in
another translation unit, as names introduced in friend declarations are
implicitly extern.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]