Skip to content

Instantly share code, notes, and snippets.

@Liam0205
Created December 26, 2018 08:39
Show Gist options
  • Save Liam0205/da536959ea2ac76634fa2acce909216d to your computer and use it in GitHub Desktop.
Save Liam0205/da536959ea2ac76634fa2acce909216d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
struct Person {
size_t age = 0;
std::string name;
Person& operator++() { // prefix
++age;
return *this;
}
Person operator++(int) { // postfix
Person res = *this; // trivial copy-constructor is called
this->operator++();
return res;
}
// not necessary here, but necessary for classes whose private members are about to be printed.
friend std::ostream& operator<<(std::ostream& os, const Person& p);
};
std::ostream& operator<<(std::ostream& os, const Person& p) {
return os << p.name << '\t' << p.age;
}
int main() {
Person liam;
liam.name = "Liam Huang";
std::cout << liam << '\n';
std::cout << ++liam << '\n';
std::cout << liam++ << '\n';
std::cout << liam << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment