C++朋友继承?

发布于 2024-12-04 04:37:40 字数 229 浏览 4 评论 0原文

子类是否继承主类的友元关联(主类自己的以及与主类成为友元的其他类)?

或者换句话说,继承如何应用于friend关键字?

展开: 如果没有的话,有什么办法可以继承友谊吗?

我按照乔恩的建议发布了设计问题:
C++ 类设计问题

Does a subclass inherit, the main class' friend associations (both the main class' own and other classes friended with the main class)?

Or to put it differently, how does inheritance apply to the friend keyword?

To expand:
And if not, is there any way to inherit friendship?

I have followed Jon's suggestion to post up the design problem:
C++ class design questions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

花开雨落又逢春i 2024-12-11 04:37:40

C++ 中的友谊不是继承的。

该标准规定(ISO/IEC 14882:2003,第 11.4.8 节):

友谊既不是继承的,也不是传递的。

Friendship is not inherited in C++.

The standard says (ISO/IEC 14882:2003, section 11.4.8):

Friendship is neither inherited nor transitive.

凉城 2024-12-11 04:37:40

您可以在父级中创建(静态)受保护的方法,这将允许您执行类似的操作。

#include <stdio.h>

class MyFriend
{
private:
    int m_member = 2;
    friend class Father;
};

class Father
{
protected:
    static int& getMyFriendMember(MyFriend& io_freind) { return io_freind.m_member; }
};

class Son : public Father
{
public:
    int doSomething(MyFriend& io_freind)
    {
        int friendMember = getMyFriendMember(io_freind);
        return friendMember;
    }
};

int main(){
    MyFriend AFriendOfFathers;
    Son aSonOfFathers;
    printf("%d\r\n", aSonOfFathers.doSomething(AFriendOfFathers));
    return 0;
}

然而,这绕过了封装,因此您可能应该重新审视您的设计。

You can create (static) protected methods in the parent that will allow you to do things like that.

#include <stdio.h>

class MyFriend
{
private:
    int m_member = 2;
    friend class Father;
};

class Father
{
protected:
    static int& getMyFriendMember(MyFriend& io_freind) { return io_freind.m_member; }
};

class Son : public Father
{
public:
    int doSomething(MyFriend& io_freind)
    {
        int friendMember = getMyFriendMember(io_freind);
        return friendMember;
    }
};

int main(){
    MyFriend AFriendOfFathers;
    Son aSonOfFathers;
    printf("%d\r\n", aSonOfFathers.doSomething(AFriendOfFathers));
    return 0;
}

This however bypasses encapsulation so you probably should take a second look at your design.

悸初 2024-12-11 04:37:40

朋友仅适用于您明确将其设为朋友的班级,而不适用于其他班级。

http://www.parashift.com/c++-faq-lite /friends.html#faq-14.4

friend only applies to the class you explicitly make it friend and no other class.

http://www.parashift.com/c++-faq-lite/friends.html#faq-14.4

自此以后,行同陌路 2024-12-11 04:37:40

答案很简单:不,子类不会继承友元关联。朋友只能访问声明关联的类的私有成员,而不能访问该类的父级和/或子级的私有成员。虽然您可能是超类的访问受保护成员,但我不确定这一点。

The answer is very simple: no, subclasses do not inherit friend associations. A friend can only access the private members of the class the association is declared in, not those of parents and/or children of that class. Although you might be access protected member of a superclass, but I'm not sure about that.

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