【发布时间】:2011-08-16 06:43:45
【问题描述】:
具体来说,我需要声明(据我所知)intrusive_ptr_{add_ref,release} 为我所引用班级的朋友:
#include <boost/intrusive_ptr.hpp>
using boost::intrusive_ptr;
class Outer {
public:
//user-exposed interface goes here
protected:
class Inner {
public:
Inner():refct(0){}
virtual ~Inner(){}
//machinery goes here
size_t refct;
};
friend void boost::intrusive_ptr_release(Inner *p);
friend void boost::intrusive_ptr_add_ref(Inner *p);
intrusive_ptr<Inner> handle;
};
namespace boost {
void intrusive_ptr_release(Outer::Inner *p){
if ((p->refct -= 1) <= 0){
delete p;
}
}
void intrusive_ptr_add_ref(Outer::Inner *p){
p->refct++;
}
};
我无法找到正确的语法来进行编译并保持我想要的访问权限。我的主要问题是 gcc 似乎对“应该在命名空间 boost 中声明 boost::intrusive_ptr_release(Outer::Inner *p) ”感到不安。
我从this example 看到 intrusive_ptr 帮助器是在命名空间 boost 中前向声明的——但我不能前向声明它们,因为据我了解,嵌套类(即“内部",这些函数引用的)只能在它们的外部类内部前向声明,这也是朋友声明必须去的地方。
各位 C++ 大师们,处理这个问题的正确方法是什么?
【问题讨论】:
标签: c++ boost namespaces smart-pointers nested-class