gpt4 book ai didi

c++ - 通过继承C++成为 friend

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:07 28 4
gpt4 key购买 nike

假设我有两个类(class)

Widget
^
|
Window

我还有另一个类应用程序:

定义如下

class Application
{
public:
...
private:
friend Widget;
};

这不会让 Window 访问 Applications protected 和 private 成员。有没有一种方法可以在不将 Window 和任何后续“Widget”声明为 Application 的友元的情况下完成此操作?

最佳答案

不,这是不可能的。

friendship 不可继承。

此外,friendship 表示两个实体之间有意强耦合因此,如果您的设计确实需要如此强的耦合,请继续让它们成为friendfriendship breaking encapsulation 是一个被误解太多的概念。

关于c++ - 通过继承C++成为 friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343439/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com