该基类的基类和派生类可以有一个公共朋友班,这是两个类的朋友吗?
我知道朋友班可以访问类的私人元素,所以我想知道天气派生的班级可以使用朋友班来访问基类的私人成员,如果朋友班级是基础类的朋友,并且派生的班级?
我是编程的完整初学者,我刚刚开始学习C ++的OOP;当我了解朋友功能/课程时,我只是想问这个问题。
I know that a friend class can access the private elements of a class, so I was wondering weather a derived class can use a friend class to access the private members of the base class, if the friend class is friends with both the base class and the derived class?
I am a complete beginner in programming and I have just started learning OOP in C++; I just wanted to ask this question as it came to my mind when I learned of friend functions/classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在C ++中,
为了授予对班级的私人或受保护成员的访问,我们应该定义朋友类或功能(或其他类的方法)。
友谊没有继承。
因此,为了访问基类和派生类的私人或受保护的成员,从另一个类中,您应该将“另一个”类定义为基类的朋友,并将其定义。
为了
“ ....如果朋友类是基类和派生类的朋友,派生的类可以使用朋友类访问基类的私人成员吗?....”问题。
友谊不是相互的。如果Y是Y的朋友,那么Y不会自动成为X的朋友。
因此,派生的类无法使用基类的朋友访问基类的私人成员。
可以使用朋友F类访问来自派生的D类B类B类的私人成员的方法。B
在末尾D可以通过类型F中的属性访问B的所有私人成员。
In C++,
In order to grant access to private or protected members of a class, we should define a friend class or a function (or a method of another class).
Friendship is not inherited.
So in order to access private or protected members of a base class and derived class, from another class you should define the "another" class as friend of both base class and it s derived class.
For the
"....can a derived class use a friend class to access the private members of the base class if the friend class is friends with both the base class and the derived class?...." question.
Friendship is not mutual. If a class Y is a friend of Y, then Y doesn’t become a friend of X automatically.
So derived class can not access private members of base class using friend of base class.
There can be a way to access private members of a base class B from derived class D by using friend class F.
At the end D can access all private members of B via the attribute in type F.
您可以在同一文件中定义同一类,以便两者可以访问朋友函数。
交易的正向声明
允许customer
类参考
事务
类,而不会导致编译器错误。如果您想进一步了解这一点,请考虑阅读一本书,因为互联网对学习并不是有时也有毒。
You can define the same class in the same file so that the two can access the friend function. The forward declaration of
Transaction
allows theCustomer
class to refer to the
Transaction
class without causing a compiler error.If you want to know more about this things consider reading a book because internet is not really good for learning also rather toxic at times.