public friend function in C++?
I saw some code in C++ and have a question about it:
class CRectangle {
int width, height;
public:
friend CRectangle duplicate (CRectangle);
};
The variables width
and height
are private, and the method duplicate()
is public. The keyword friend
is for accessing the private and protected functions outside of a CRectangle
object, but duplicate()
is already public. If it can be accessed from the outside, why do we need "friend"?
Does this mean that, although we directly call a public method like duplicate()
outside of the class, if the method accesses some private variable, it is also not allowed and we have to declare it as "friend?"
This is the implementation of the duplicate()
function; I just want to make the question clear:
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
retu开发者_C百科rn (rectres);
}
If you remove friend
, this function will become a method - a function
, that is declared inside the class and is a public method
(as there's public:
)
If you put friend
in front of duplicate
, this means that you declare a function, that is not a member of the class, that takes one argument - CRectangle
and that have access to private
/protected
members of the class.
The second case requires definition for CRectangle duplicate( CRectangle )
Both cases are different.
EDIT:
For friend
, the access specifier doesn't matter.
By "The second case requires definition for CRectangle duplicate( CRectangle )
", I mean, that the first case requires definition, too, but it is
// vvvvvvvvvvvv
CRectangle CRectangle::duplicate( CRectangle )
The Golden Rule is:
Friend functions can be declared under any Access Specifier, the access specifier rules do not apply to friend functions.
In your example,
friend CRectangle duplicate (CRectangle);
is declared as friend function under public
access specifier but it would still behave exactly the same manner even if declared under a private
access specifier.
You are mistaking the function to be as a member function and hence the question. It is NOT.The function is returning a object of the type CRectangle
and is friend of the same class CRectangle
.
The function CRectangle duplicate (CRectangle)
can access all the private & protected
members of the class CRectangle
because it is declared friend of the class
not because it is declared under the public access specifier.
Simple: duplicate
is not a member of CRectangle
. You are declaring duplicate
as a free function, which function is a friend of CRectangle
.
class C {
public:
friend void F(); // declares non-member F
void F(); // declares member F
};
Its a good question, the syntax is a little confusing.
The syntax does not mean that duplicate
is a member function of the class CRectangle
. Rather it means that the function (regular function) duplcate
has access to the private details of CRectangle
. Its indicating that "permission is granted to the duplicate function".
The function duplicate
may still needs to be declared and defined as any other regular function (outside the CRectangle
class). I prefer to do this as a reminder that its a regular free function
精彩评论