重载 = 运算符时返回对象的深层副本
因此,我为整数创建了一个容器类,并且想要重载 =
运算符,以便可以返回对象的深层副本。我的代码有效,但两个对象指向相同的地址。这是 main.cpp 文件:
int main (int argc, const char * argv[]) {
IntList cArray(5);
for (int i = 0; i < cArray.getLength(); i++) {
cArray[i] = (i + 1) * 10;
}
using namespace std;
for (int i = 0; i < cArray.getLength(); i++)
cout << cArray[i] << " ";
cout << endl << popped << endl;
IntList cArray2(4);
for (int i = 0; i < cArray2.getLength(); i++)
cArray2[i] = i * 5;
cArray2 = cArray;
cArray2[2] = 1000;
for (int i = 0; i < cArray.getLength(); i++)
cout << cArray[i] << " ";
cout << endl;
for (int i = 0; i < cArray2.getLength(); i++)
cout << cArray2[i] << " ";
cout << endl;
return 0;
}
这是 IntList
类的头文件:
class IntList {
private:
int _length;
int* _data;
public:
IntList(int length);
~IntList();
void erase();
void reallocate(int length); // Faster way to call erase() and resize()
void resize(int length);
void insert(int value, int index);
void prepend(int value);
void append(int value);
int pop(int index);
void removeBefore(int index); // Exclusive
void removeAfter(int index); // Exclusive
int getLength();
int indexOf(int value);
int& operator[](int index);
IntList operator=(IntList* source);
};
这是 IntClass
的 operator=()
的实现代码>方法:
IntList IntList::operator=(IntList* source) {
_length = source->getLength();
reallocate(_length);
for (int i = 0; i < _length; i++) {
_data[i] = (*source)[i];
}
return *this;
}
So I making a container class for integers and I want to overload the =
operator so that I can return a deep copy of the object. My code works but the two objects point to the same address. This is the main.cpp file:
int main (int argc, const char * argv[]) {
IntList cArray(5);
for (int i = 0; i < cArray.getLength(); i++) {
cArray[i] = (i + 1) * 10;
}
using namespace std;
for (int i = 0; i < cArray.getLength(); i++)
cout << cArray[i] << " ";
cout << endl << popped << endl;
IntList cArray2(4);
for (int i = 0; i < cArray2.getLength(); i++)
cArray2[i] = i * 5;
cArray2 = cArray;
cArray2[2] = 1000;
for (int i = 0; i < cArray.getLength(); i++)
cout << cArray[i] << " ";
cout << endl;
for (int i = 0; i < cArray2.getLength(); i++)
cout << cArray2[i] << " ";
cout << endl;
return 0;
}
This is the header file for the IntList
class:
class IntList {
private:
int _length;
int* _data;
public:
IntList(int length);
~IntList();
void erase();
void reallocate(int length); // Faster way to call erase() and resize()
void resize(int length);
void insert(int value, int index);
void prepend(int value);
void append(int value);
int pop(int index);
void removeBefore(int index); // Exclusive
void removeAfter(int index); // Exclusive
int getLength();
int indexOf(int value);
int& operator[](int index);
IntList operator=(IntList* source);
};
And this is the implementation of IntClass
's operator=()
method:
IntList IntList::operator=(IntList* source) {
_length = source->getLength();
reallocate(_length);
for (int i = 0; i < _length; i++) {
_data[i] = (*source)[i];
}
return *this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为您的赋值运算符采用指向 IntList 的指针,所以您需要像这样调用它:
您的示例代码使用编译器生成的默认赋值运算符。您的赋值运算符应该具有以下声明:
Because your assignment operator takes a pointer to an IntList, you would need to call it like this:
Your example code is using the default assignment operator generated by your compiler. Your assignment operator should have this declaration instead:
您不使用指向
IntList
的指针 -operator=
通常采用const &
并返回对所分配实例的引用。请记住,您还需要一个复制构造函数:
IntList(IntList const & source)
您可以创建一个带有指向 IntList 指针的运算符= - 只有在您这样做时才有效做了这样的事情:
这不是典型的用法,如果您需要这样做,您应该明确地表示,在这种情况下使用例如
void IntList::copyFrom(IntList const *)
。您应该进行的其他更改:
添加以下内容:
将这些设置为常量:
You're not working with pointers to
IntList
-operator=
typically takes aconst &
and returns a reference to the instance being assigned to.Remember that you also need a copy constructor:
IntList(IntList const & source)
You can make an operator= which takes a pointer to IntList - that would only work if you did something like this:
This isn't typical usage, and you should rather be explicit if you require this, use e.g.
void IntList::copyFrom(IntList const *)
in this case.Other changes you should make:
Add this:
Make these const:
operator=
的签名错误,因为它的参数类型是指向IntList
的指针正确的签名是这样的:
也就是说,同时设置两个参数类型作为返回类型引用。
Wrong signature for
operator=
, as it's parameter type is a pointer toIntList
Correct signature is this:
That is, make both parameter type as well as return type reference.
您的操作员需要签名
IntList&运算符=(const IntList&源);
。请注意引用而不是指针,并且您还必须通过引用返回以允许赋值链接。当您通过指针将其传递到任何需要隐式赋值的地方时,将使用编译器生成的浅复制赋值运算符。编辑:您还需要使
getLength
const
以便能够在赋值运算符内调用它。Your operator needs the signature
IntList& operator=(const IntList& source);
. Note the reference instead of the pointer, and that you must RETURN by reference as well to allow assignment chaining. When you pass it by pointer anywhere implicit assignment is required the compiler-generated shallow copy assignment operator will be used.EDIT: You also need to make
getLength
const
so that it's able to be called inside the assignment operator.