検索しましたが、「いつ」使用するかの答えが見つかりません。余分なコピーを節約できるので、良いと聞いています。私は持っていたすべてのクラスにそれを入れてみましたが、いくつかのクラスではそれが意味をなさないようでした:S LValues と RValues と std::move 対 std::copy 対 memcpy に関する無数のチュートリアルを読みましたvs. memmove など。throw() についても読んでいますが、それをいつ使用するかはわかりません。
私のコードは次のようになります:
struct Point
{
int X, Y;
Point();
Point(int x, int y);
~Point();
//All my other operators here..
};
次に、そのようなクラス配列があります(RAIIのようなもの):
class PA
{
private:
std::vector<Point> PointsList;
public:
PA();
//Variadic Template constructor here..
~PA();
//Operators here..
};
ムーブ コンストラクターとコピー コンストラクターを使用する必要がありますか? ポイントクラスで持っていたのですが、変な感じがしたので外しました。それからPAクラスで持っていたのですが、あまり役に立たないと思ったので外しました。次に、私のビットマップ クラスで、コンパイラはポインター メンバーを持っているがオーバーロードがないことについて不平を言っていたので、次のようにしました。
//Copy Con:
BMPS::BMPS(const BMPS& Bmp) : Bytes(((Bmp.width * Bmp.height) != 0) ? new RGB[Bmp.width * Bmp.height] : nullptr), width(Bmp.width), height(Bmp.height), size(Bmp.size), DC(0), Image(0)
{
std::copy(Bmp.Bytes, Bmp.Bytes + (width * height), Bytes);
BMInfo = Bmp.BMInfo;
bFHeader = Bmp.bFHeader;
}
//Move Con:
BMPS::BMPS(BMPS&& Bmp) : Bytes(nullptr), width(Bmp.width), height(Bmp.height), size(Bmp.size), DC(0), Image(0)
{
Bmp.Swap(*this);
Bmp.Bytes = nullptr;
}
//Assignment:
BMPS& BMPS::operator = (BMPS Bmp)
{
Bmp.Swap(*this);
return *this;
}
//Not sure if I need Copy Assignment?
//Move Assignment:
BMPS& BMPS::operator = (BMPS&& Bmp)
{
this->Swap(Bmp);
return *this;
}
//Swap function (Member vs. Non-member?)
void BMPS::Swap(BMPS& Bmp) //throw()
{
//I was told I should put using std::swap instead here.. for some ADL thing.
//But I always learned that using is bad in headers.
std::swap(Bytes, Bmp.Bytes);
std::swap(BMInfo, Bmp.BMInfo);
std::swap(width, Bmp.width);
std::swap(height, Bmp.height);
std::swap(size, Bmp.size);
std::swap(bFHeader, Bmp.bFHeader);
}
これは正しいです?私は何か悪いことをしましたか?throw() は必要ですか? 私の代入演算子とムーブ代入演算子は、実際にはそのように同じである必要がありますか? コピー課題は必要ですか?ああ、たくさんの質問があります :c 私が最後に質問したフォーラムではすべてに答えられなかったので、混乱したままでした。最後に、Bytes に unique_ptr を使用する必要がありますか? (これはバイト/ピクセルの配列です。)