我有一个叫做的类Location,我需要在其成员变量中添加一个CArray.此更改导致需要重载赋值运算符.
有没有办法复制在我进行更改之前复制的此类类型中的所有变量,只需添加其他代码来复制CArray而不单独复制每个成员变量?
Location& Location::operator=(const Location &rhs)
{
// Only do assignment if RHS is a different object from this.
if (this != &rhs)
{
//Copy CArray
m_LocationsToSkip.Copy(rhs.m_LocationsToSkip);
//Copy rest of member variables
//I'd prefer not to do the following
var1 = rhs.var1;
var2 = rhs.var2;
//etc
}
return *this;
}
Run Code Online (Sandbox Code Playgroud)
是的,有点像。使用重载自身的类型operator=,因此您不必在包含的类中执行此操作。即使在编写MFC代码时,我仍然主要使用std::vector、std::string等,而不是MFC集合和字符串类。有时您几乎无法使用CString,但我不记得上次使用CArray而不是std::vector。
| 归档时间: |
|
| 查看次数: |
1646 次 |
| 最近记录: |