C ++ 11编译器自动知道何时使用移动分配以及何时使用常规分配。如果应用程序已经定义了普通分配和移动分配运算符,则显式编写std :: move是否有意义/有区别。
CMyVehicleList类既具有移动功能,又具有普通的复制和分配功能
class CMyVehicleList
{
public :
CMyVehicleList() {// some code}
CMyVehicleList(const CMyVehicleList& rhs) {// some code}
CMyVehicleList(CMyVehicleList&& rhs) {// some code}
CMyVehicleList operator = (const CMyVehicleList& rhs) {// some code}
CMyVehicleList operator = (CMyVehicleList&& rhs) {// some code}
};
全局函数根据传递给该函数的所有者返回CMyVehicleList对象。
MyVehicleList GetVehicleLists(const string& owner)
{
MyVehicleList Temp;
if (owner == "mark")
{
// pupulate MyVehicleList with marks details...
}
else if (owner == "daniel")
{
// pupulate MyVehicleList with daniel details...
}
return Temp
}
在给定的情况下,以下两个调用之间有什么区别
MyVenicalList userlist;
userlist = GetVehicleList("mark");
userlist = std::move(GetVehicleList("mark"));