限制方法对C ++中特定类的访问 [英] Restrict method access to a specific class in C++

查看:80
本文介绍了限制方法对C ++中特定类的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个密切相关的类,我将它们称为Widget和Sprocket. Sprocket具有一组我希望可以从Widget而不是其他任何类调用的方法.我也不想只是将Widget声明为Spocket的朋友,因为这会使Widget访问所有受保护的成员和私有成员.我想将Widget的访问权限限制为仅特定的一组方法.

I have two closely related classes which I'll call Widget and Sprocket. Sprocket has a set of methods which I want to be callable from Widget but not from any other class. I also don't want to just declare Widget a friend of Spocket because that would give Widget access to ALL protected and private members. I want to restrict Widget's access to only a specific set of methods.

我想到的一个解决方案是在Sprocket中创建一个嵌套类,其中包含这些方法的包装,并使Widget成为该嵌套类的朋友.例如:

One solution I came up with is to create a nested class inside Sprocket that contains wrappers for these methods and make Widget a friend of this nested class. For example:

class Sprocket
{
public:
    class WidgetInterface
    {
        friend class Widget;
        WidgetInterface(Sprocket* parent) : mParent(parent) {}

    private:
        void A() { mParent->A(); }
        void B() { mParent->B(); }

        Sprocket* mParent;
    };

private:
    void A() { ... }
    void B() { ... }
};

class Widget
{
public:
    Widget(Sprocket* sprock) : mSprocketIface(sprock) {}

    void doStuff() { mSprocketIface.A(); }  // Widget can call Sprocket::A()

private:
    Sprocket::WidgetInterface mSprocketIface;
};

这会导致一些代码重复,因为方法签名现在在两个地方声明了,但是可以工作.但是现在假设我想添加一个名为SpecialWidget的Widget子类,并且我希望该类也可以访问Sprocket方法.我可以简单地将此新类添加到Sprocket朋友列表中,也可以在Widget中添加另一组受保护的包装,SpecialWidget(以及任何其他子类)可以访问这些包装,但是您现在可以看到这已成为维护问题.如果添加新类或更改方法签名,则不需要更新朋友列表或包装器.如果我使用添加另一套包装器"方法,则方法签名将在三个位置重复!

This results in some code duplication because the method signatures are now declared in two places, but it works. But now suppose I want to add a subclass of Widget called SpecialWidget and I want that class to also have access to the Sprocket methods. I can simply add this new class to the Sprocket friends list or I can add yet another set of protected wrappers in Widget that SpecialWidget (and any other subclass) can access but you can see that this is now becoming a maintenance issue. I don't want to have to update the friends list or the wrappers if I add new classes or change the method signature. If I use the "add another set of wrappers" approach, the method signatures will be duplicated in three places!

有人知道这样做更简单,更干净吗?

Does anyone know of a simpler, cleaner way to do this?

推荐答案

如果您有两个紧密耦合的类,那么实际上不值得尝试使friend访问的粒度比实际更细.您可以控制两者的实现,并且应该对自己足够信任,不要滥用严格地说不需要调用某些方法的能力.

If you have two tightly coupled classes, then it's really not worth trying to make friend access any more granular than it is. You control the implementation of both, and you should trust yourself enough to not abuse the ability to call some methods that you don't, strictly speaking, need to call.

如果您想让以后的代码维护者更清楚,请在friend声明中添加注释,以说明其中的原因(通常是个好主意),以及允许朋友类调用哪些私有方法.

If you want to make it clear for future code maintainers, add a comment to the friend declaration explaining why it is there (a good idea in general), and what private methods are allowed to be called by the friend class.

这篇关于限制方法对C ++中特定类的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆