将前向声明的类的成员函数声明为friend [英] Declare a member-function of a forward-declared class as friend

查看:296
本文介绍了将前向声明的类的成员函数声明为friend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将前向声明的类的成员函数声明为friend?我试图做以下:

  class BigComplicatedClass; 

class Storage {
int data_;
public:
int data(){return data_; }
// OK,但提供了过宽的访问:
friend class BigComplicatedClass;
//错误无效使用不完全类型:
friend void BigComplicatedClass :: ModifyStorage();
};因此,目标是(i)将朋友声明限制为单个方法,以及(ii)将朋友声明限制为单个方法,不包括复杂类的定义以减少编译时间。



一种方法可能是添加一个充当中介的类:

  //在Storage.h中:
class BigComplicatedClass_Helper;
class Storage {
//(...)
friend class BigComplicatedClass_Helper;
};

//在BigComplicatedClass.h中:
class BigComplicatedClass_Helper {
static int& AccessData(Storage& storage){return storage.data_; }
friend void BigComplicatedClass :: ModifyStorage();
};

但是,这看起来有点笨重...所以我认为必须有一个更好的解决方案!

解决方案

As @Ben说,这是不可能的,但你可以通过密钥。它的工作原理有点像中间辅助类,但是更清晰:

  // Storage.h 
向前声明密钥
class StorageDataKey;

class Storage {
int data_;
public:
int data(){return data_; }
//只有可以将密钥传递给此函数的函数具有访问
//并获取数据作为引用
int& data(StorageDataKey const&){return data_; }
};

// BigComplicatedClass.cpp
#includeBigComplicatedClass.h
#includeStorage.h

//定义密钥
class StorageDataKey {
StorageDataKey(){} // default ctor private
StorageDataKey(const StorageDataKey&){} // copy ctor private

//授予访问权限方法
friend void BigComplicatedClass :: ModifyStorage();
};

void BigComplicatedClass :: ModifyStorage(){
int& data = storage_.data(StorageDataKey());
// ...
}


Is it possible to declare a member function of a forward-declared class as friend? I am trying to do the following:

class BigComplicatedClass;

class Storage {
   int data_;
public:
   int data() { return data_; }
   // OK, but provides too broad access:
   friend class BigComplicatedClass;
   // ERROR "invalid use of incomplete type":
   friend void BigComplicatedClass::ModifyStorage(); 
};

So the goal is to (i) restrict the friend declaration to a single method, and (ii) not to include the definition of the complicated class to reduce compile time.

One approach might be to add a class acting as an intermediary:

// In Storage.h:
class BigComplicatedClass_Helper;
class Storage {
    // (...)
    friend class BigComplicatedClass_Helper;
};

// In BigComplicatedClass.h:
class BigComplicatedClass_Helper {
     static int &AccessData(Storage &storage) { return storage.data_; }
     friend void BigComplicatedClass::ModifyStorage();
};

However, this seems a bit clumsy... so I assume that there must be a better solution!

解决方案

As @Ben says, it's not possible, but you can give specific access just to that member function through a "passkey". It works a bit like the intermediate helper class, but is imho clearer:

// Storage.h
// forward declare the passkey
class StorageDataKey;

class Storage {
   int data_;
public:
   int data() { return data_; }
   // only functions that can pass the key to this function have access
   // and get the data as a reference
   int& data(StorageDataKey const&){ return data_; }
};

// BigComplicatedClass.cpp
#include "BigComplicatedClass.h"
#include "Storage.h"

// define the passkey
class StorageDataKey{
  StorageDataKey(){} // default ctor private
  StorageDataKey(const StorageDataKey&){} // copy ctor private

  // grant access to one method
  friend void BigComplicatedClass::ModifyStorage();
};

void BigComplicatedClass::ModifyStorage(){
  int& data = storage_.data(StorageDataKey());
  // ...
}

这篇关于将前向声明的类的成员函数声明为friend的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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