Created
March 10, 2014 15:56
-
-
Save mryoshio/9467790 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class person { | |
private: | |
int age; | |
string name; | |
public: | |
person() : age(0), name("") {}; | |
person(int a, string s) : age(a), name(s) {}; | |
person(const person& p) : age(p.age), name(p.name) { | |
cout << "// copy constructor passed." << endl; | |
}; | |
person& operator=(person& pp) { | |
cout << "// copy assignment passed." << endl; | |
age = pp.age; | |
name = pp.name; | |
return *this; | |
} | |
person(person&& p) : age(p.age), name(p.name) { | |
cout << "// move constructor passed." << endl; | |
}; | |
person& operator=(person&& pp) { | |
cout << "// move assignment passed." << endl; | |
age = pp.age; | |
name = pp.name; | |
return *this; | |
} | |
~person() {}; | |
void introduce() { cout << "My name is " << name << endl; } | |
}; | |
int main() { | |
// simple use | |
cout << "####### p" << endl; | |
person p(53, "taro"); | |
p.introduce(); | |
// use copy constructor | |
cout << "####### p2" << endl; | |
person p2 = p; | |
p2.introduce(); | |
// use copy assignment | |
cout << "####### p3" << endl; | |
person p3; | |
p3 = p; | |
p3.introduce(); | |
// use move constructor | |
cout << "####### p4" << endl; | |
person p4(move(p)); | |
p4.introduce(); | |
// use move assignment | |
cout << "####### p5" << endl; | |
person p5; | |
p5 = move(p); | |
p5.introduce(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
imiwakaran