C++如何优雅地对待不被继承的友谊
我有一组类,
class myClassA{
friend class MyFatherClass;
};
class MyFatherClass{
...
};
class MySonClass : public MyFatherClass {
};
我的父类可以访问类MyClassA的所有方法。 我还希望所有扩展 MyFatherClass 的类都能够调用此类方法。
我只能看到 2 个选项:
- 我可以随时在 myClassA 中将新班级添加为好友。 (我不喜欢它)
- 我在父函数中创建一些受保护的包装器来访问类 myClassA 中的方法。 (稍微好一点,但我仍然不喜欢它,因为我必须在 myClassA 中创建新方法时随时创建一个新包装器)
您是否有任何更优雅的解决方案来解决该问题?
谢谢
I have a set of classes
class myClassA{
friend class MyFatherClass;
};
class MyFatherClass{
...
};
class MySonClass : public MyFatherClass {
};
My father class can access all the methods of the class MyClassA.
I would like as well that all the class which will extend MyFatherClass will be able to call such methods.
I can see just 2 options:
- at any time I go to add in myClassA the new class as a friend. ( I do not like it )
- I create some protected wrapper in the father function to access the method from the class myClassA. (slightly better but i still do not like it as well because i have to create a new wrapper at any time a new method is created in myClassA)
Do you have any idea for a more elegant solution to the problem?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先……优雅是什么意思?您需要编写的代码更少?我建议您在可读性方面不要妥协。
利用友谊不应该是一个轻易做出的决定。 SO 上有很多线程处理这个问题,但在这里我假设你已经知道这意味着什么。
选项 1) 更具可读性。当有人看到该类时,他们将直接知道谁可以访问该类。代码应该具有表现力,并且此选项完美地描述了意图。
选项2)有点过分了。您正在编写一个包装器,以便可以访问某些函数...为什么不首先将它们设为公共,因为包装器具有公共访问权限。它只是徒劳地添加了一个抽象层。
您应该首先考虑功能(两者都有效)、表现力和可读性(这里选项 1 肯定更好)。
First off... what does elegant mean? Less code for you to write? I suggest you don't compromise when it comes to readability.
Using friendship should not be a decision taken lightly. There are numerous threads on SO dealing with this, but here I'll just assume you already know what this implies.
Option 1) is a lot more readable. When someone sees the class, they will directly know who has access to it. Code should be expressive, and this option describes the intent perfectly.
Option 2) is a bit of an overkill. You're writing a wrapper just so you can access some functions... why not make them
public
to start with, since the wrapper has public access. It's just an added layer of abstraction for nothing.You should first think about functionality (both work), expressiveness and readability (option 1 is definitely better here).
对应用程序知之甚少的情况下做出判断有点困难,但如果这些函数应该仅由
MyFatherClass
及其后代使用,那么它们应该受到保护
成员(可能是MyFatherClass
的静态
)。也许
MyClassA
应该是MyFatherClass
的成员,没有自己的成员函数,只是一个struct
来保存一些数据成员。只是一个建议……由于信息太少,很难判断什么是最好的。一般的想法是,该语言不允许
友谊继承
,因为没有它你总是可以做出好的设计。It's a bit hard to make a judgment knowing so little about the application, but if the functions should only be used by
MyFatherClass
and its descendents, they should beprotected
members (perhapsstatic
) ofMyFatherClass
.Perhaps
MyClassA
should be a member ofMyFatherClass
with no member functions of its own, just astruct
to hold some data members.Just a suggestion… it's hard to tell what's best given so little information. The general idea is that the language disallows
friend
ship inheritance because you can always make a good design without it.