Question about friend function.
Hi, everyone!
I have often noticed that someone declares a friend
fucntion inside a class (in the .h file of the class)
and give the definition of the function in the .cpp file
of the same class. I want to know whether this
method is correct? Does it have some weak points? Where should
correct place which we give the definition (Note: not the declaration)
of the function?
Another question is, if I define the function like below, what is the
scope of the function? (i.e. where can compiler find the function?)
Here is the sample codes I noticed:
in A.h
--------
class A {
friend int functionA();
void memberfunction();
}
--------
A.cpp
--------
A::memberfunction()
{
//implemention
}
int functionA()
{
//implemention
}
--------
Thanks in advance,
George
Re: Re: Re: Re: Re: George
Thanks for all of your help, Gabriel buddy!
regards,
George
Quote:
Originally posted by Gabriel Fleseriu
It was never my intention to say they were the same. They are definitively not. The one difference you named is correct. There also are others, for example class access qualifiers(public/protected/private) don't apply to friends, and friends are not inherited. To ask the same question the other way around: what are the similarities between a friend function and a static member function? Or, more generally, why can we say that a friend function is actually part of the class? What is a class, actually? Try to think of these questions not from the point of view of the language syntax, but from that of what a class is supposed to model.