【发布时间】:2015-08-26 10:13:23
【问题描述】:
C++11 移动和复制赋值运算符应该返回 const 吗?
示例代码:
const my_class& operator=(const my_class& other)
{
// Copy the data
my_class tmp(other);
*this = std::move(tmp);
return *this;
}
const my_class& operator=(my_class&& other)
{
// Steal the data
std::swap(my_data_length, other.my_data_length);
std::swap(my_data, other.my_data);
return *this;
}
为了清楚起见:
class my_class
{
protected:
double *my_data;
uint64_t my_data_length;
}
注意返回类型上的const。我通常会不假思索地提出这个问题,但真的有必要吗?返回const 会阻止您做什么? (这里有一个例子会很有帮助。)
请注意,给出了一个示例,但随后将其删除。任何人都可以评论以下内容吗? (a = b).non_const_member_function();
【问题讨论】:
标签: c++11