重载 = 运算符时返回对象的深层副本

发布于 2024-10-27 15:08:04 字数 1854 浏览 6 评论 0原文

因此,我为整数创建了一个容器类,并且想要重载 = 运算符,以便可以返回对象的深层副本。我的代码有效,但两个对象指向相同的地址。这是 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);
};

这是 IntClassoperator=() 的实现代码>方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不爱素颜 2024-11-03 15:08:04

因为您的赋值运算符采用指向 IntList 的指针,所以您需要像这样调用它:

cArray2 = &cArray;

您的示例代码使用编译器生成的默认赋值运算符。您的赋值运算符应该具有以下声明:

IntList& IntList::operator=(IntList const& source)

Because your assignment operator takes a pointer to an IntList, you would need to call it like this:

cArray2 = &cArray;

Your example code is using the default assignment operator generated by your compiler. Your assignment operator should have this declaration instead:

IntList& IntList::operator=(IntList const& source)
似梦非梦 2024-11-03 15:08:04

您不使用指向 IntList 的指针 - operator= 通常采用 const & 并返回对所分配实例的引用。

IntList & IntList::operator=(IntList const & source) {
  ...
  return *this;
}

请记住,您还需要一个复制构造函数: IntList(IntList const & source)

可以创建一个带有指向 IntList 指针的运算符= - 只有在您这样做时才有效做了这样的事情:

IntList l1;
IntList l2;
l1 = &l2;

这不是典型的用法,如果您需要这样做,您应该明确地表示,在这种情况下使用例如 void IntList::copyFrom(IntList const *)

您应该进行的其他更改:

添加以下内容:

int operator[](int index) const;

将这些设置为常量:

int getLength() const;
int indexOf(int value) const;

You're not working with pointers to IntList - operator= typically takes a const & and returns a reference to the instance being assigned to.

IntList & IntList::operator=(IntList const & source) {
  ...
  return *this;
}

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:

IntList l1;
IntList l2;
l1 = &l2;

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:

int operator[](int index) const;

Make these const:

int getLength() const;
int indexOf(int value) const;
魔法唧唧 2024-11-03 15:08:04
IntList IntList::operator=(IntList* source) 

operator= 的签名错误,因为它的参数类型是指向 IntList指针

正确的签名是这样的:

IntList & IntList::operator=(const IntList & source) //reference of source!
     //^^^ note this                      ^^^ note this as well!

也就是说,同时设置两个参数类型作为返回类型引用

IntList IntList::operator=(IntList* source) 

Wrong signature for operator=, as it's parameter type is a pointer to IntList

Correct signature is this:

IntList & IntList::operator=(const IntList & source) //reference of source!
     //^^^ note this                      ^^^ note this as well!

That is, make both parameter type as well as return type reference.

半仙 2024-11-03 15:08:04

您的操作员需要签名 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文