这种面向关键的访问保护模式是已知的习语吗? [英] Is this key-oriented access-protection pattern a known idiom?
问题描述
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屋!