#include #include #include #include "Message.h" int Message::instance = 0; void printv(Message msg) { cout << "Function printv: " << msg << endl; } void printr(Message& msg) { cout << "Function printr: " << msg << endl; } void printm(Message&& msg) { cout << "Function printm: " << msg << endl; } int main() { cout << "Default constructor:" << endl; Message msg; cout << "msg = " << msg << endl; cout << endl << "Regular constructor:" << endl; Message hello1("hello"); Message world1("world"); cout << "hello1 = " << hello1 << endl; cout << "world1 = " << world1 << endl; cout << endl << "Copy constructor:" << endl; Message hello2(hello1); cout << "hello1 = " << hello1 << endl; cout << "hello2 = " << hello2 << endl; cout << endl << "Move constructor:" << endl; Message hello3(move(hello1)); // make hello1 an rvalue cout << "hello1 = " << hello1 << endl; cout << "hello3 = " << hello3 << endl; cout << endl << "Copy assignment operator:" << endl; Message world2; world2 = world1; cout << "world1 = " << world1 << endl; cout << "world2 = " << world2 << endl; cout << endl << "Move assignment operator #1:" << endl; Message helloworld1; helloworld1 = hello2 + world2; // the expression is an rvalue cout << "helloworld1 = " << helloworld1 << endl; cout << " hello2 = " << hello2 << endl; cout << " world2 = " << world2 << endl; cout << endl << "Move assignment operator #2:" << endl; Message helloworld2; helloworld2 = add(hello2, world2); // the return value is an rvalue cout << "helloworld2 = " << helloworld2 << endl; cout << " hello2 = " << hello2 << endl; cout << " world2 = " << world2 << endl; cout << endl << "Move assignment operator #3:" << endl; Message world3; world3 = move(world1); // make world1 an rvalue cout << "world1 = " << world1 << endl; cout << "world3 = " << world3 << endl; cout << endl << "Moved return rvalue #1:" << endl; Message hi("Hi"); Message mom("Mom"); Message himom(hi + mom); // the return rvalue is moved cout << "himom = " << himom << endl; cout << endl << "Moved return rvalue #2:" << endl; Message go("Go"); Message spartans("Spartans"); Message gospartans(add(go, spartans)); // the return rvalue is moved cout << "gospartans = " << gospartans << endl; cout << endl << "Append an lvalue to a vector:" << endl; vector v; v.reserve(2); v.push_back(gospartans); cout << "v[0] = " << v[0] << endl; cout << endl << "Append an rvalue to a vector:" << endl; v.push_back(go + spartans); cout << "v[1] = " << v[1] << endl; cout << endl << "Print by value #1:" << endl; printv(hello2); cout << endl << "Print by value #2:" << endl; printv(hello2 + world2); cout << endl << "Print by reference:" << endl; printr(hello2); cout << endl << "Print by move #1:" << endl; printm(hello2 + world2); cout << endl << "Print by move #2:" << endl; printm(move(hello2)); printr(hello2); cout << endl << "Program terminating." << endl; return 0; }