Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created March 10, 2014 15:56
Show Gist options
  • Save mryoshio/9467790 to your computer and use it in GitHub Desktop.
Save mryoshio/9467790 to your computer and use it in GitHub Desktop.
#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;
}
@mryoshio
Copy link
Author

imiwakaran

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment