1

I've seen a pattern like this in some project:

class AccessKey{
    // a group of classes called privilegedClasses 
    friend class foo;
    friend class bar;
    // friend class other classes in privilegedClasses...
    private :
        AccessKey(){}; /*! private constructor */
        static const AccessKey key; /*! private object, only for friend classes */
}

This is a class that only privilegedClasses can access to it and have an object of it. Now consider someone is writing a function and want to limit access to that function to classes in privilegedClasses. She can do it simply by adding an AccessKey object to arguments. like this:

void functionForPrivilegedClassses(AccessToken token, ...){
}

Therefore only classes in privilegedClasses can call this function because only they can have an object of that class.
Invocation would be like this: functionForPrivilegedClasses(AccessKey::key,...)
I want to know is it a good practice generally? Is there a better way to achieve this?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Alireza Mirian
  • 5,862
  • 3
  • 29
  • 48
  • 1
    [Friendship and the Attorney-Client Idiom](http://www.drdobbs.com/friendship-and-the-attorney-client-idiom/184402053) – user703016 Jun 02 '14 at 11:21
  • a) If you want to protect methods, put them into the protected section of a class. If the user (of the lib etc.) *wants* to work around it, he is reponsible for errors too. b) Your method isn´t safe too, only more complicated – deviantfan Jun 02 '14 at 11:22
  • Why would you need the key, if the function is private, then only friends can access it. No need for the key, the private restriction is enough, unless I am missing something. – Ólafur Waage Jun 02 '14 at 11:23
  • 1
    @ÓlafurWaage: With this pattern you can have many unrelated restricted functions, with a single list of classes able to access them. – Quentin Jun 02 '14 at 11:26
  • Yes, for the Attorney-Client idiom, I did not read that from the OP's question. From his view he has no need for the key if I see it correctly. But doing the AC idiom you need a second class that has only private methods (which are the privileged methods) and the 3rd class as his friend. – Ólafur Waage Jun 02 '14 at 11:29
  • @WilliamAndrewMontgomery Good article, thanks. I read it, but I think it was a little different approach and for slightly different purpose. I think Attorney-Client aims to solve the problem of _all or nothing_ friendship where it's not exactly the case in the approach I described. In other words, Attorney-Client pattern, give access of some functions of a **specific class** to a group of classes by means of an attorney class, however, in the above approach any function (from any class) can restrict its access to a group of classes by means of a key. – Alireza Mirian Jun 17 '14 at 10:14
  • @ÓlafurWaage exactly! This is kind of omitting a (probably large) block of friendship declarations by adding an extra key argument. – Alireza Mirian Jun 17 '14 at 10:27

0 Answers0