C++ 中的公共友元函数?
我看到了一些 C++ 代码,对此有一个疑问:
class CRectangle {
int width, height;
public:
friend CRectangle duplicate (CRectangle);
};
变量 width
和 height
是私有的,而方法 duplicate()
是公共的。关键字 friend
用于访问 CRectangle
对象外部的私有和受保护函数,但 duplicate()
已经是公共的。如果可以从外部访问,为什么还需要“朋友”?
这是否意味着,虽然我们直接在类外部调用像 duplicate()
这样的公共方法,但如果该方法访问某些私有变量,也是不允许的,我们必须将其声明为“friend” ?”
这是duplicate()
函数的实现;我只是想把问题说清楚:
CRectangle duplicate (CRectangle rectparam)
{
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
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;
return (rectres);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果删除
friend
,此函数将成为一个方法 - 一个function
,它在类内部声明,并且是一个public 方法
(因为有public:
)如果你把
friend
放在duplicate
前面,这意味着你声明一个函数,即不是类的成员,它采用一个参数 -CRectangle
并且可以访问该类的private
/protected
成员。第二种情况需要定义
C矩形重复(CRectangle)
两种情况是不同的。
编辑:
对于
friend
,访问说明符并不重要。通过“第二种情况需要定义
CRectangle重复(CRectangle)
”,我的意思是,第一种情况也需要定义,但它是If you remove
friend
, this function will become a method - afunction
, that is declared inside the class and is apublic method
(as there'spublic:
)If you put
friend
in front ofduplicate
, this means that you declare a function, that is not a member of the class, that takes one argument -CRectangle
and that have access toprivate
/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黄金规则是:
可以在任何访问说明符下声明友元函数,访问说明符规则不适用于友元函数。
在您的示例中,
在下声明为友元函数
public
访问说明符,但即使在private
访问说明符下声明,它的行为方式仍然完全相同。您将该函数误认为是成员函数,因此出现了问题。它是NOT。该函数返回一个
CRectangle
类型的对象,并且是同一个类CRectangle
的友元。函数
CRectangle副本(CRectangle)
可以访问所有private & protected
类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,
is declared as friend function under
public
access specifier but it would still behave exactly the same manner even if declared under aprivate
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 classCRectangle
.The function
CRectangle duplicate (CRectangle)
can access all theprivate & protected
members of the classCRectangle
because it is declaredfriend of the class
not because it is declared under the public access specifier.简单:
duplicate
不是CRectangle
的成员。您将duplicate
声明为自由函数,该函数是CRectangle
的友元。Simple:
duplicate
is not a member ofCRectangle
. You are declaringduplicate
as a free function, which function is a friend ofCRectangle
.这是一个好问题,语法有点混乱。
该语法并不意味着
duplicate
是CRectangle
类的成员函数。相反,它意味着函数(常规函数)duplcate
可以访问CRectangle
的私有详细信息。其表示“已授予重复功能许可”。函数
duplicate
可能仍然需要声明并定义为任何其他常规函数(在CRectangle
类之外)。我更喜欢这样做是为了提醒它是一个常规的免费功能Its a good question, the syntax is a little confusing.
The syntax does not mean that
duplicate
is a member function of the classCRectangle
. Rather it means that the function (regular function)duplcate
has access to the private details ofCRectangle
. Its indicating that "permission is granted to the duplicate function".The function
duplicate
maystill needs tobe declared and defined as any other regular function (outside theCRectangle
class). I prefer to do this as a reminder that its a regular free function