c***z 发帖数: 6348 | 1 按照一般的做法,我在class A里面declare了private的copy constructor, 没有给
definition.
然后我照此处理class B, 可是B里面包括一个指向A的指针, 我得到了这个error:
cannot access private members defined in B
(还有一个error指向xmemory的208行, 就是new那行
template
void construct(pointer _Ptr, _Other&& _Val)
{ // construct object at _Ptr with value _Val
::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Other>(_Val));
})
是不是因为我把A的copy constructor禁了? 请问有什么办法解决么? 谢谢各位大侠! | b*******s 发帖数: 5216 | 2 按你的描述,一点问题都没有,我用的代码如下,用g++编译的
class A
{
private:
A(const A&);
A& operator= (const A&);
};
class B
{
private:
A* pA;
};
int main()
{
B b;
return 0;
}
如果你是试图去初始化pA,最好你显式定义一个A的构造器,带参的拷贝构造器一声明
,就把缺省构造器隐藏了 | G*****7 发帖数: 1759 | 3 not directly answering your question, but a c++11-compliant compiler such as
gcc/clang/icl has provided the syntax sugar of "= delete". that ought to
give you more diagonostic error messages wrt cp-ctor.
instead of
class foo
{
foo(const foo &);
}
you could do
class foo
{
foo(const foo &) = delete;
}
【在 c***z 的大作中提到】 : 按照一般的做法,我在class A里面declare了private的copy constructor, 没有给 : definition. : 然后我照此处理class B, 可是B里面包括一个指向A的指针, 我得到了这个error: : cannot access private members defined in B : (还有一个error指向xmemory的208行, 就是new那行 : template : void construct(pointer _Ptr, _Other&& _Val) : { // construct object at _Ptr with value _Val : ::new ((void _FARQ *)_Ptr) _Ty(_STD forward<_Other>(_Val)); : })
| h**********c 发帖数: 4120 | 4 that is the job of factory pattern.
simply put a public static member function of the class that fabricates
instances of that class.
Viewer discretion advised. I don't disambiguate any mis-conception. |
|