2010-05-25 65 views
2

是否有可能做到便携如下:C++友元类的std :: vector的

struct structure { 
    structure() {} 
private: 
    // only allow container copy construct 
    structure(const structure&) {} 
    // in general, does not work because allocator (not vector) calls copy construct 
    friend class std::vector<structure>; 
}; 

例如消息尝试上述编译:

In member function void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) 
[with _Tp = kernel_data<const double*>::block]: 
... 
/usr/include/c++/4.3/ext/new_allocator.h:108: error: within this context 

感谢

我有解决办法,但我很好奇这是怎么可能的

回答

3

vector(更确切地说,一个可以将施工任务委托给自由职能或其他职业,使得船舶无用。

即使您传递了自己的分配器,它也可能会反弹到实现的内部类中。然后你的类的构造函数可以从该类访问,而不是你的分配器。所以如果这是您的解决方法,那么不能保证。 (虽然看GCC的实现,但它确实使用un rebound分配器来构造这样的子对象)。

在GCC的libstdC++中,没有STL容器模板构造了标准类或函数范围内的包含对象。

+0

不,我的解决方法是放弃完全复制构造函数。 谢谢 – Anycorn 2010-05-25 19:03:35

+1

@aaa:如何在不使用复制构造函数的情况下使用容器内的类?更有可能你正在使用隐式拷贝构造函数。 – Potatoswatter 2010-05-25 19:09:23