#include <iostream>
static int i=0;
using namespace std;
class Movable
{
public:
Movable ():mId(++i){
cout<<"constructing it "<<mId<<endl;
};
Movable (const Movable&)=delete;
Movable (Movable&)=delete;
void operator=(Movable&)=delete;
void operator=(const Movable&)=delete;
Movable (const Movable&& aObject)
{
cout<<"Moving it constant "<<mId<<endl;
// mId=++i;
};
Movable (Movable&&aObject)
{
cout<<"Moving it "<<mId<<endl;
};
Movable &operator=( Movable&&aObject)
{
cout<<"Moving it assignment "<<mId<<endl;
return *this;
}
Movable &operator=(const Movable&&aObject)
{
cout<<"Moving it assignment constant "<<mId<<endl;
return *this;
}
~Movable ()
{
cout<<"destroying it "<<mId<<endl;
}
int getId() const {
return mId;
}
private:
int mId;
};
Movable&& CreatenNewMovable ()
{
Movable lM;
return std::move(lM);
}
int main() {
Movable a;
a=CreatenNewMovable();
return 0;
}
The output result of this code is
constructing it 1 constructing it 2 destroying it 2 Moving it assignment 1 destroying it 1
I'm a little bit confused how is it possible to destroy the temp object then move it to second. Is that an undefined behavior ? m I missing something about the move operation?
Aucun commentaire:
Enregistrer un commentaire