コンテナを構築するには、明らかに標準コンテナの 1 つ (std::vector など) を使用する必要があります。ただし、これは、オブジェクトに RAW ポインターが含まれている場合に考慮する必要があることの完璧な例です。
オブジェクトに RAW ポインターがある場合は、3 のルールを覚えておく必要があります (C++11 では 5 のルールになりました)。
- コンストラクタ
- デストラクタ
- コンストラクターのコピー
- 代入演算子
- コンストラクターの移動 (C++11)
- 代入の移動 (C++11)
これは、定義されていない場合、コンパイラがこれらのメソッドの独自のバージョンを生成するためです (以下を参照)。コンパイラで生成されたバージョンは、RAW ポインターを処理するときに常に役立つとは限りません。
コピー コンストラクターは、正しく取得するのが難しいものです (強力な例外保証を提供したい場合は、簡単ではありません)。内部でコピー アンド スワップ イディオムを使用できるため、代入演算子はコピー コンストラクターの観点から定義できます。
整数の配列へのポインターを含むクラスの絶対最小値の詳細については、以下を参照してください。
それを正しくするのは簡単ではないことを知っているので、整数の配列へのポインターではなく std::vector の使用を検討する必要があります。ベクターは使いやすく (そして拡張しやすく)、例外に関連するすべての問題をカバーします。次のクラスを以下の A の定義と比較してください。
class A
{
std::vector<int> mArray;
public:
A(){}
A(size_t s) :mArray(s) {}
};
あなたの問題を見る:
A* arrayOfAs = new A[5];
for (int i = 0; i < 5; ++i)
{
// As you surmised the problem is on this line.
arrayOfAs[i] = A(3);
// What is happening:
// 1) A(3) Build your A object (fine)
// 2) A::operator=(A const&) is called to assign the value
// onto the result of the array access. Because you did
// not define this operator the compiler generated one is
// used.
}
コンパイラで生成された代入演算子は、ほぼすべての状況で問題ありませんが、RAW ポインターが使用されている場合は注意が必要です。あなたの場合、浅いコピーの問題が原因で問題が発生しています。同じメモリ部分へのポインターを含む 2 つのオブジェクトが作成されました。A(3) がループの最後で範囲外になると、そのポインターで delete [] が呼び出されます。したがって、(配列内の) もう一方のオブジェクトには、システムに返されたメモリへのポインターが含まれています。
コンパイラが生成したコピー コンストラクター。そのメンバーのコピー コンストラクターを使用して、各メンバー変数をコピーします。ポインターの場合、これはポインター値がソース オブジェクトから宛先オブジェクトにコピーされることを意味します (つまり、浅いコピー)。
コンパイラが代入演算子を生成しました。そのメンバー代入演算子を使用して、各メンバー変数をコピーします。ポインターの場合、これはポインター値がソース オブジェクトから宛先オブジェクトにコピーされることを意味します (つまり、浅いコピー)。
したがって、ポインターを含むクラスの最小値は次のとおりです。
class A
{
size_t mSize;
int* mArray;
public:
// Simple constructor/destructor are obvious.
A(size_t s = 0) {mSize=s;mArray = new int[mSize];}
~A() {delete [] mArray;}
// Copy constructor needs more work
A(A const& copy)
{
mSize = copy.mSize;
mArray = new int[copy.mSize];
// Don't need to worry about copying integers.
// But if the object has a copy constructor then
// it would also need to worry about throws from the copy constructor.
std::copy(©.mArray[0],©.mArray[c.mSize],mArray);
}
// Define assignment operator in terms of the copy constructor
// Modified: There is a slight twist to the copy swap idiom, that you can
// Remove the manual copy made by passing the rhs by value thus
// providing an implicit copy generated by the compiler.
A& operator=(A rhs) // Pass by value (thus generating a copy)
{
rhs.swap(*this); // Now swap data with the copy.
// The rhs parameter will delete the array when it
// goes out of scope at the end of the function
return *this;
}
void swap(A& s) noexcept
{
using std::swap;
swap(this.mArray,s.mArray);
swap(this.mSize ,s.mSize);
}
// C++11
A(A&& src) noexcept
: mSize(0)
, mArray(NULL)
{
src.swap(*this);
}
A& operator=(A&& src) noexcept
{
src.swap(*this); // You are moving the state of the src object
// into this one. The state of the src object
// after the move must be valid but indeterminate.
//
// The easiest way to do this is to swap the states
// of the two objects.
//
// Note: Doing any operation on src after a move
// is risky (apart from destroy) until you put it
// into a specific state. Your object should have
// appropriate methods for this.
//
// Example: Assignment (operator = should work).
// std::vector() has clear() which sets
// a specific state without needing to
// know the current state.
return *this;
}
}