Направих просто приложение с конструктор за копиране на преместване и оператор за присвояване на копие за преместване и за всеки от тях направих изявление cout само за да ми каже кои се изпълняват. Но по време на изпълнение не видях никакво изявление от операторите за копиране на движение, а само от този по подразбиране, който компилаторът вече предоставя. Ето моят код:
#include <iostream>
#include <string>
using namespace std;
class Names{
private:
string* name;
public:
Names(string place_name){
cout << "Overloaded constructor" << endl;
name = new string;
*name = place_name;
}
//copy constructor
Names(Names& cpy){
cout << "Copy constructor" << endl;
name = new string;
*name = *cpy.name;
}
//assignment
Names& operator =(const Names& cpy){
cout << "Assignment operator" << endl;
name = new string;
*name = *cpy.name;
return *this;
}
//move constructor
Names(Names&& cpym){
cout << "Copy move constructor" << endl;
name = cpym.name;
cpym.name = NULL;
}
//move assignment operator
Names& operator=(Names&& cpym){
cout << "Copy assignment operator" << endl;
delete name;
name = cpym.name;
cpym.name = NULL;
return *this;
}
//destructor
~Names(){
cout << "Deallocating memory" << endl;
delete [] name;
}
};
int main(){
Names nme("Bob");
Names copin("something");
copin = nme;
system("pause");
return 0;
}
и ето изхода
така че основният въпрос е
1) Why isn't the cout statement being show for the move constructor?
2) Is my declaration for move constructor correct
и благодаря ти.
nullptr
. В противен случай не съм сигурен защо изобщо очаквате конструкторът за преместване да бъде извикан и компилаторът все още може да генерира версии за преместване вместо вас. - person chris   schedule 06.03.2014