相关疑难解决方法(0)

我们能否增加这种面向密钥的访问保护模式的可重用性?

我们是否可以增加这种面向密钥的访问保护模式的可重用性:

class SomeKey { 
    friend class Foo;
    // more friends... ?
    SomeKey() {} 
    // possibly non-copyable too
};

class Bar {
public:
    void protectedMethod(SomeKey); // only friends of SomeKey have access
};
Run Code Online (Sandbox Code Playgroud)

为了避免持续的误解,这种模式不同于律师 - 客户的习惯用语:

  • 它可以比律师 - 客户更简洁(因为它不涉及通过第三类代理)
  • 它可以允许授权访问权限
  • ...但它对原始类也更具侵入性(每个方法一个虚拟参数)

(在这个问题中提出了一个侧面讨论,因此我打开了这个问题.)

c++ design-patterns idioms friend access-protection

26
推荐指数
1
解决办法
4417
查看次数

C++ 中带有友元方法的两个类

目前我正在阅读一本书C++,它有一些练习。其中一个练习要求构建两个类,其中每个类都有一个用于另一个类的朋友方法。我目前的猜测是这样的:

#include <iostream>

using std::cout;
using std::endl;

class Y;

class X{
public:
   void friend Y::f(X* x);
   void g(Y* y){cout << "inside g(Y*)" << endl;}
};

class Y{
public:
   void friend X::g(Y* y);
   void f(X* x) {cout << "inside f(X*)"  << endl;}
};
Run Code Online (Sandbox Code Playgroud)

但我的猜测无法编译,因为类Xvoid friend Y::f(X* x);方法声明。我怎样才能解决这个难题?请再给我一些猜测。

c++ friend friend-class

5
推荐指数
1
解决办法
1361
查看次数