【发布时间】:2019-12-04 18:33:18
【问题描述】:
所以这是一个用户定义向量类的复制分配示例 [摘自 Bjarne Stroustrup 的“C++ 之旅(第 2 版)”:
Vector& Vector::operator=(const Vector& a) // copy assignment
{
double* p = new double[a.sz];
for (int i=0; i!=a.sz; ++i)
p[i] = a.elem[i];
delete[] elem; // delete old elements
elem = p; // here elem is the vector's data holding member array
sz = a.sz;
return *this;
}
'This' 是一个指针,所以解引用它实际上应该给我们它指向的当前对象。在预期对所述对象的引用时,如何将对象作为返回值接受?
【问题讨论】:
标签: c++ class operator-overloading this assignment-operator