干净的C ++细粒度的朋友等效吗? (答:律师 - 客户成语) [英] clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)

查看:79
本文介绍了干净的C ++细粒度的朋友等效吗? (答:律师 - 客户成语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C ++有 public 可以调用的成员,朋友公开所有 private 外国类或方法的成员,但不提供任何语法来向特定的呼叫者揭露特定成员?

Why does C++ have public members that anyone can call and friend declarations that expose all private members to given foreign classes or methods but offer no syntax to expose particular members to given callers?

我想用一些例程来表达接口,只能由已知的调用者调用,而不必给这些调用者完全访问所有私有,这感觉是一个合理的事情。最好的我可以想出自己(下面)和他人的建议迄今为止围绕不同间接的成语/模式,我真的只想要一种方式来单一,明确指出的简单类定义什么呼叫者(比我,我的孩子绝对任何人更细粒度)可以访问哪些成员。表达这个概念的最好方式是什么?

I want to express interfaces with some routines to be invoked only by known callers without having to give those callers complete access to all privates, which feels like a reasonable thing to want. The best I could come up with myself (below) and suggestions by others so far revolve around idioms/pattern of varying indirectness, where I really just want a way to have single, simple class definitions that explicitly indicate what callers (more granularly than me, my children, or absolutely anybody) can access which members. What is the best way to express the concept below?

// Can I grant Y::usesX(...) selective X::restricted(...) access more cleanly?
void Y::usesX(int n, X *x, int m) {
  X::AttorneyY::restricted(*x, n);
}

struct X {
  class AttorneyY;          // Proxies restricted state to part or all of Y.
private:
  void restricted(int);     // Something preferably selectively available.
  friend class AttorneyY;   // Give trusted member class private access.
  int personal_;            // Truly private state ...
};

// Single abstract permission.  Can add more friends or forwards.
class X::AttorneyY {
  friend void Y::usesX(int, X *, int);
  inline static void restricted(X &x, int n) { x.restricted(n); }
};

我没有一个软件组织大师,但感觉就像界面简洁和原则最小的特权直接在语言这个方面是不合适的。我的愿望的一个更清晰的例子可能是一个 Person 类,声明的方法如 takePill(Medicine *) tellTheTruth() forfeitDollars(unsigned int)只有 Physician 判断 TaxMan 实例/成员方法分别应该考虑调用。每个主要界面方面需要一次性代理或接口类与我不利,但如果你知道我错过了某些东西,请说出来。

I'm nowhere near being a software organization guru, but it feels like interface simplicity and the principle of least privilege are directly at odds in this aspect of the language. A clearer example for my desire might be a Person class with declared methods like takePill(Medicine *) tellTheTruth() and forfeitDollars(unsigned int) that only Physician, Judge, or TaxMan instances/member methods, respectively, should even consider invoking. Needing one-time proxy or interface classes for each major interface aspect sits ill with me, but please speak up if you know I'm missing something.

答案从 Drew Hall 接受: Dobbs博士 - 友谊和律师 - 客户端成语

Answer accepted from Drew Hall: Dr Dobbs - Friendship and the Attorney-Client Idiom

上面的代码最初称为包装类代理,而不是'律师'并用指针而不是引用,但是与Drew发现的相反,这就是我所认为的最通用的解决方案。 (不要在背上太笨了...)我也改变了限制的签名来演示参数转发。这个成语的总体成本是每个权限集一个一个一个朋友的声明,每个批准的呼叫者一个朋友声明,每个权限集一个转发包装每个暴露方法。下面的大部分更好的讨论围绕转发呼叫样板,一个非常相似的密钥成语避免了以较少的直接保护为代价。

The code above originally called the wrapper class 'Proxy' instead of 'Attorney' and used pointers instead of references but was otherwise equivalent to what Drew found, which I then deemed the best generally known solution. (Not to pat myself on the back too hard...) I also changed the signature of 'restricted' to demonstrate parameter forwarding. The overall cost of this idiom is one class and one friend declaration per permission set, one friend declaration per set approved caller, and one forwarding wrapper per exposed method per permission set. Most of the better discussion below revolves around the forwarding call boilerplate that a very similar 'Key' idiom avoids at the expense of less direct protection.

推荐答案

律师客户端成语可能是您要查找的内容。机制与您的成员代理类解决方案没有太大的不同,但是这种方法更为惯用。

The Attorney-Client idiom may be what you're looking for. The mechanics are not too different from your member proxy class solution, but this way is more idiomatic.

这篇关于干净的C ++细粒度的朋友等效吗? (答:律师 - 客户成语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆