I want to use C++11 move semantics. And I wrote the following class:
class ColorM
{
public:
ColorM(float _r, float _g, float _b, float _a){
qDebug()<<"Constructor";
r = _r;
g = _g;
b = _b;
a = _a;
m = new float[16];
}
ColorM(const ColorM &other){
qDebug()<<"Copy Constructor";
}
~ColorM(){
if (m != nullptr)
{
qDebug()<<"Deleting resource.";
// Delete the resource.
delete[] m;
}
}
// Move constructor.
ColorM(ColorM&& other)
{
qDebug()<<"Move Constructor";
r = other.r;
g = other.g;
b = other.b;
a = other.a;
m = other.m;
other.m = nullptr;
}
float r;
float g;
float b;
float a;
float *m;
private:
};
When I try to:
std::vector<ColorM> vec;
vec.push_back(ColorM(0.1, 0.6, 0.3, 0.7));
vec.push_back(ColorM(0.2, 0.6, 0.3, 0.7));
vec.push_back(ColorM(0.3, 0.6, 0.3, 0.7));
I got copy constructor calls. What I doing wrong?
I have used this as example. And compile it with g++.
Here is QT project I used for my tests: http://wikisend.com/download/261514/MoveConstructor.zip