0

=そこで、整数のコンテナクラスを作成し、オブジェクトのディープコピーを返すことができるように、演算子をオーバーロードしたいと思います。私のコードは機能しますが、2つのオブジェクトが同じアドレスを指しています。これは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;
}
4

4 に答える 4

2

IntList-へのポインタを操作していません。operator=通常はを取り、const &割り当てられているインスタンスへの参照を返します。

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

コピーコンストラクタも必要であることを忘れないでください。IntList(IntList const & source)

IntListへのポインタを受け取るoperator=を作成できます。これは、次のようなことをした場合にのみ機能します。

IntList l1;
IntList l2;
l1 = &l2;

これは一般的な使用法ではありません。これが必要な場合は、明示する必要があります。たとえばvoid IntList::copyFrom(IntList const *)、この場合は使用してください。

行う必要のあるその他の変更:

これを追加:

int operator[](int index) const;

これらをconstにします:

int getLength() const;
int indexOf(int value) const;
于 2011-03-25T19:02:00.947 に答える
2

代入演算子はIntListへのポインターを受け取るため、次のように呼び出す必要があります。

cArray2 = &cArray;

サンプルコードは、コンパイラによって生成されたデフォルトの代入演算子を使用しています。代わりに、代入演算子には次の宣言が必要です。

IntList& IntList::operator=(IntList const& source)
于 2011-03-25T19:06:13.337 に答える
1

オペレーターには署名が必要IntList& operator=(const IntList& source);です。ポインタの代わりに参照に注意してください。割り当ての連鎖を可能にするには、参照によっても戻る必要があることに注意してください。暗黙の代入が必要な場所にポインタで渡すと、コンパイラが生成した浅いコピー代入演算子が使用されます。

getLength const編集:代入演算子内で呼び出せるようにする必要もあります。

于 2011-03-25T19:02:19.720 に答える
1
IntList IntList::operator=(IntList* source) 

operator=パラメータタイプがへのポインタであるため、の署名が間違っていますIntList

正しい署名は次のとおりです。

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

つまり、パラメータ型と戻り型参照の両方を作成します。

于 2011-03-25T19:03:39.090 に答える