C++ 有办法吗?渲染一个类'除了少数类之外,所有类的接口都是私有的?
我正在编写一个 B-link 树及其伴随的子类,例如数据页类和节点类等。
我想知道是否有一种方法可以保护节点和页面的公共接口,以便只有 b-link 树类本身可以访问它们,而无需同时将页面和节点的私有方法暴露给 b-link 类?
IE 我已经考虑过简单地将页面和节点的“公共”接口更改为受保护类别,然后将 B-link 树声明为友元,但这使 b-link 树可以访问我想要保留的私有方法私人的。
I am writing a B-link tree and its attendant sub classes like a data page class and a node class etc.
I was wondering is there a way to protect the public interfaces of the nodes and pages such that only the b-link tree class itself can access them, WITHOUT simultaneously exposing the private methods of the pages and nodes to the b-link class?
IE I have already thought of simply changing the 'public' interface of the pages and nodes into the protected category and then declaring the B-link tree as a friend but that gives the b-link tree access to private methods that I want to stay private.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我的脑海中,你可以做类似的事情:(
未编译或测试,但你应该明白这个想法。)
Off the top of my head, you could do something like:
(Not compiled or tested, but you should get the idea.)
您可以尝试在与 b 树相同的翻译单元中的匿名名称空间中定义伴随子类。据说这将使这些类无法从该翻译单元外部访问。
请参阅未命名/匿名命名空间与静态函数
You can try defining your attendant sub classes in an anonymous namespace in the same translation unit as the b-tree. Supposedly that will make those clases inaccesible from outside that translation unit.
See Unnamed/anonymous namespaces vs. static functions
如果您不希望在 API 中公开节点和页面的接口,那么只需在 b-link 实现文件中声明它们即可。如果 b-link 实现跨越多个文件,则将节点和页面类声明放入仅实现的头文件中(与实现文件位于同一位置,而不是与 API 头文件位于同一位置)。
If you don't want the interfaces of the nodes and pages to be exposed in your API then just declare them inside your b-link implementation file. If the b-link implementation spans more than one file, then put the node and page class declarations in an implementation-only header file (co-located with your implementation files, not with your API headers).