C++ 中的公共友元函数?

发布于 2024-11-05 08:16:00 字数 737 浏览 3 评论 0原文

我看到了一些 C++ 代码,对此有一个疑问:

  class CRectangle {
        int width, height;
      public:
        friend CRectangle duplicate (CRectangle);
    };

变量 widthheight 是私有的,而方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回眸一笑 2024-11-12 08:16:00

如果删除 friend,此函数将成为一个方法 - 一个 function,它在类内部声明,并且是一个 public 方法(因为有public: )

如果你把 friend 放在 duplicate 前面,这意味着你声明一个函数,即不是类的成员,它采用一个参数 - CRectangle 并且可以访问该类的 private/protected 成员。

第二种情况需要定义C矩形重复(CRectangle)

两种情况是不同的。


编辑:
对于friend,访问说明符并不重要。

通过“第二种情况需要定义CRectangle重复(CRectangle)”,我的意思是,第一种情况也需要定义,但它是

//         vvvvvvvvvvvv
CRectangle CRectangle::duplicate( CRectangle )

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 )
灯下孤影 2024-11-12 08:16:00

黄金规则是:

可以在任何访问说明符下声明友元函数,访问说明符规则不适用于友元函数。

在您的示例中,

friend CRectangle duplicate (CRectangle); 

在下声明为友元函数public 访问说明符,但即使在 private 访问说明符下声明,它的行为方式仍然完全相同。

您将该函数误认为是成员函数,因此出现了问题。它是NOT。该函数返回一个CRectangle类型的对象,并且是同一个类CRectangle的友元。

函数CRectangle副本(CRectangle)可以访问所有private & protectedCRectangle 成员,因为它被声明为该类的友元,而不是因为它是在公共访问说明符下声明的。

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.

彼岸花ソ最美的依靠 2024-11-12 08:16:00

简单:duplicate 不是 CRectangle 的成员。您将 duplicate 声明为自由函数,该函数是 CRectangle 的友元。

class C {
public:
    friend void F(); // declares non-member F
    void F(); // declares member F
};

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
};
冰火雁神 2024-11-12 08:16:00

这是一个好问题,语法有点混乱。

该语法并不意味着duplicateCRectangle 类的成员函数。相反,它意味着函数(常规函数)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 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文