Matthieu M.在我之前見過的這個答案中提出了一種訪問保護模式,但是從來沒有自覺地考慮過一種模式:class SomeKey { friend class Foo; SomeKey() {} // possibly make it non-copyable too};class Bar {public: void protectedMethod(SomeKey);};在這里,只有一個friend密鑰類可以訪問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一個friend的Bar,避免了更多的復雜模式進行代理。有誰知道這種方法是否已經有名稱,即已知模式?
- 3 回答
- 0 關注
- 649 瀏覽
添加回答
舉報
0/150
提交
取消