这种面向密钥的访问保护模式是一种已知的习惯用法吗? [英] Is this key-oriented access-protection pattern a known idiom?

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

问题描述

Matthieu M.这个答案 我以前见过,但从未有意识地考虑过一种模式:

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);
};

这里只有key类的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 成为 Barfriend 相比,它允许更细粒度的访问控制,并避免更复杂的代理模式.

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天全站免登陆