未调用重载的赋值运算符

Vis*_*shu 1 c++ assignment-operator

我编写了一个重载的赋值运算符,类perform复制所有变量值.例如:在Exp.cpp中

class perform
{
    LOG *ptr;
int a;
//constructor
//destructor
perform operator=(const perform & rhs){

   ptr = rhs.ptr; a=rhs.s;
return * this;}
};
Run Code Online (Sandbox Code Playgroud)

在另一个类中output,我已经声明了一个指针abc.

perform * ptr = StatCol::CreateCol(frm);
abc = ptr; //this line should invoke assignment overloaded.
           //but in my case it's not invoked.
Run Code Online (Sandbox Code Playgroud)

小智 9

假设abc是Perform对象,则需要取消引用要分配的指针:

abc = * ptr;
Run Code Online (Sandbox Code Playgroud)

如果abc本身是一个指针,那么你不能按照你的要求去做 - 你不能在LHS是一个指针的情况下重载赋值.您必须取消引用两个指针:

* abc = * ptr;
Run Code Online (Sandbox Code Playgroud)