这种面向关键的访问保护模式是已知的习语吗? [英] Is this key-oriented access-protection pattern a known idiom?

查看:99
本文介绍了这种面向关键的访问保护模式是已知的习语吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matthieu M. 这个答案,我曾经看到,但从来没有conciously考虑一个模式:

Matthieu M. brought up a pattern for access-protection in this answer that i'd seen before, but never conciously considered a pattern:

class SomeKey { 
    friend class Foo;
    SomeKey() {} 
    // possibly make it non-copyable too
};

class Bar {
public:
    void protectedMethod(SomeKey);
};

这里只有键类别的 friend 可访问 protectedMethod()

Here only a friend of the key class has access to protectedMethod():

class Foo {
    void do_stuff(Bar& b) { 
        b.protectedMethod(SomeKey()); // fine, Foo is friend of SomeKey
    }
};

class Baz {
    void do_stuff(Bar& b) {
        b.protectedMethod(SomeKey()); // error, SomeKey::SomeKey() is private
    }
};

它允许更精细的访问控制比 Foo a c> c>,避免更复杂的代理模式。

It allows more fine-granular access-control than making Foo a friend of Bar and avoids more complicated proxying patterns.

有人知道这种方法是否已经有一个名字,即是一个已知的模式?

Does anyone know whether this approach already has a name, i.e., is a known pattern?

推荐答案

您的其他问题,它看起来像是这种模式

Thanks to your other question it looks like this pattern is now known as the "passkey" pattern.

在C ++ 11中,它更清晰,因为它不是调用

In C++11, it gets even cleaner, because instead of calling

b.protectedMethod(SomeKey());

您可以直接致电:

b.protectedMethod({});

这篇关于这种面向关键的访问保护模式是已知的习语吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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