限制对类方法的访问

发布于 2024-12-28 00:18:37 字数 727 浏览 1 评论 0原文

我有一个类 A,它具有公共方法,并由在不同应用程序中实现的 100 个其他类使用。现在我想将这些公共方法设置为私有方法,以便没有新的类访问它们,但我希望现有的客户端类仍然可以访问它们。 但我什至不想碰那些客户类,因为所有者很少允许他们的类出现任何波动。

我检查了

我可以从在课堂之外不借助朋友?

C++:有吗一种限制对某些类的某些方法的访问而不暴露其他私有成员的方法?

friend访问权限有限的类

但所有(并非所有)都要求更改客户端代码。客户端代码不应更改。

一种直接的方法是让所有 N 类成为朋友,但我不太愿意这样做。是否有任何模式或可接受的技术(请不要破解)来实现此访问限制? 谢谢您,如果这是重复的,我深表歉意。

I have a class A which has public methods and used by 100 other classes implemented in different applications. Now I want to make those public methods as private so that no new classes access them, but I want the existing client classes to still access them.
But I don't want to even touch those client classes , because the owners seldom allow even any ripple in their classes.

I checked

Can I access private members from outside the class without using friends?

C++: Is there a way to limit access to certain methods to certain classes without exposing other private members?

friend class with limited access

But all ( not all really ) demand a change in the client's code. The client code should not change.

One straight forward way is to make all those N classes friends , But I am somewhat not comfortable doing that. Is there any pattern or an acceptable technique ( not a hack please ) to achieve this access restriction?
Thank you and I apologize if this is a duplicate.

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

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

发布评论

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

评论(2

墟烟 2025-01-04 00:18:37

C++ 中的类被成为友元,以表明类之间存在特殊的强耦合。 friend 的这种使用实际上增强了封装性,而不是像流行的感觉那样破坏它。
怎么办?
如果没有友谊,向其他类公开功能的唯一非黑客方法是提供 publicgetset 成员函数,这在事实上打破了封装,因为所有类(甚至那些不需要的类)现在都可以访问这些方法,因此成员增加了潜在破坏类数据的风险。

回到您的情况,如果您有 100 个类需要访问该特定类,那么您已经通过将这些方法设置为 public 来进行正确的设计。现在尝试使这些方法对未来的类私有是一种尝试破解您现有的设计,您的设计不支持它。

将现有类设为friend并不完全符合上述标准,因此对于该场景来说不是一个好的选择。
但是,鉴于这种情况,您没有其他方法可以实现此目的。将现有类设为friend并授予它们特殊访问权限似乎是唯一的方法。这仍然很糟糕,因为只能访问少数方法的 100 个类现在可以访问整个类。

Classes in C++ are made friends in order to indicate an special intentional strong coupling between classes. This use of friend infact enhances Encapsulation rather than break it as maybe the popular feeling.
How?
Without friendship the only non-hack way to expose the functionality to other class would be to provide public, get and set member functions,this in fact breaks encapsulation because all classes(even those who don't need to) now have access to these methods and hence the members increasing the risk of potentially breaking down the class data.

Back to your situation, If you have a 100 classes which need access to this particular class, then you already had the right design in-place by having those methods as public. Now trying to make those methods private to future classes is a trying to hack your existing design, Your design does not support it.

Making the existing classes as friends does not ideally fit in the above mentioned criteria and hence is not a good choice for the scenario.
However, given the situation there is no other way in which you can implement this. Making the existing classes as friend and granting them the special access seems the only way. This is still bad because the 100 classes which only had access to the few methods will now have access to your entire class.

羁〃客ぐ 2025-01-04 00:18:37

我认为你可以提取 A 类的接口(让它成为 IA)并让 A 实现 IA >。您根本不应该在 IA 中定义这些公共方法。

然后,旧代码将继续使用 A 并可以访问 A 公共方法,而新代码将使用受限接口,该代码将通过某些 Fabric 接收。

当然,如果您需要(复制)构造类或类似的东西,这可能是无法实现的,但我现在不能在不知道类的用法的情况下说出来。

此外,由于虚拟函数,您会获得一些开销

I think you can extract an interface of the A class (let it be IA) and make A to implement IA. You should not define those public methods in IA at all.

Then, old code will continue using A and will have access to A public methods, while new code will use restricted interface, that code would receive through some fabric .

Of cause, this can be unimplementable, if you need to (copy-)construct class, or smth like this, but I can't say it now without knowing the usage of class.

Also, you get a little overhead due to virtual functions

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