When to use Move Constructors / Assignments

I searched, but cannot find the answer to “When,” to use them. I just keep hearing that it’s good, because it saves me an extra copy. I went around it in every class that I had, but some, as it did not look for some classes: S I read countless tutorials on LValues ​​and RValues ​​and std :: move vs. std :: copy vs. memcpy vs. memmove etc. And even read on throw (), but I'm not sure when to use this.

My code looks like this:

struct Point
{
    int X, Y;

    Point();
    Point(int x, int y);
    ~Point();

    //All my other operators here..
};

Then I have an array of classes similar to (RAII sorta thing):

class PA
{
    private:
        std::vector<Point> PointsList;

    public:
        PA();
        //Variadic Template constructor here..
        ~PA();
        //Operators here..
 };

? Point Class, , . 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);
}

? - ? ()? ? ? Ahh : c , , , . , unique_ptr Bytes? ( /.)

+5
2

:

-, . rvalues ​​ . -, , , . std:: array. -, , , . : std::string. , , std::string SSO ( ), , !

, , , . , , /, . , .

, , . / , r . " " .

, Dave Abraham post .

, . , .

+8

- , , . () ( , , ). . , , .

, . unique_ptr, shared_ptr, ( ). Unique_ptr, ( ). - , .

Move is great, - , , msvc / , (/ ..) . , , , . , , , , , , (.. vector.push_back, emplace_back, , ), ( SSO, ).

boost / / /, , .

+2

All Articles