#include <iostream> class Data { public: Data() { std::cout << "EMPTY." << std::endl; } Data(const Data &d) { std::cout << "lvalue copy." << std::endl; } Data(const Data &&d) { std::cout << "rvalue copy." << std::endl; } Data &operator=(const Data &d) { std::cout << "lvalue assign." << std::endl; return *this; } Data &operator=(const Data &&d) { std::cout << "rvalue assign." << std::endl; return *this; } }; int main(int argc, char *argv[]) { std::cout << "data0: "; Data data0; std::cout << "data1: "; Data data1 = data0; std::cout << "data2: "; Data data2(data0); std::cout << "data3: "; Data data3 = std::move(data0); std::cout << "data4: "; Data data4(std::move(data1)); std::cout << "data5: "; Data data5; data5 = data2; std::cout << "data6: "; Data data6; data6 = std::move(data2); }
运行结果:
data0: EMPTY. data1: lvalue copy. data2: lvalue copy. data3: rvalue copy. data4: rvalue copy. data5: EMPTY. lvalue assign. data6: EMPTY. rvalue assign.
标签:形参,右值,lvalue,rvalue,构造,copy,EMPTY From: https://www.cnblogs.com/qiandeheng/p/17448409.html