开发者

Returning deep copies of objects when overloading the = operator

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;
}


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;


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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜