How to name this key-oriented access-protection pattern?

前端 未结 4 826
花落未央
花落未央 2020-12-03 05:21

Apparently this key-oriented access-protection pattern:

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

class B         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 05:57

    There's other ways to do this, in a more general fashion, using inheritance. Here, class cake functions as both the keyhole and the key. Here, any class that inherits (could also be static inheritance) from cake can access the subset of SomeClass defined to be accessible in cake, and of course, you could have multiple different subsets in multiple different classes.

    class cake;
    class SomeClass {
        friend class cake;
        void foo();
    };
    class cake {
        void DoFoo(SomeClass& class) { class.foo(); }
    };
    class lols : cake {
        // Now we can DoFoo().
    };
    

    I'd name it lock and key.

提交回复
热议问题