Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Class {
- int i;
- char *p;
- public:
- int geti() const { return i; }
- void increment() { ++i; }
- void decrement() { --i; }
- void addr() const { std::cout << reinterpret_cast<void *>(p) << std::endl; }
- void swap(Class &other) { std::swap(i, other.i); std::swap(p, other.p); }
- // - Constructors
- // --- Default
- Class() : i(0), p(new char[10]) { std::cout << "Default ctor" << std::endl; }
- Class(int val) : i(val), p(new char[10]) { std::cout << "ctor(int) with " << val << std::endl; }
- // --- Copy
- Class(const Class &other) : i(other.i), p(new char[10]) { std::cout << "Copy ctor" << std::endl; }
- // --- Move
- Class(Class &&other) : p(nullptr) { other.swap(*this); std::cout << "Move ctor" << std::endl; }
- // - Assignment
- // --- Copy
- Class &operator=(const Class &other) {
- std::cout << "operator=(const Class&)" << std::endl;
- if (this != &other)
- Class(other).swap(*this);
- return *this;
- }
- // --- Move
- Class &operator=(Class &&other) {
- std::cout << "operator=(Class&&)" << std::endl;
- if (this != &other)
- other.swap(*this);
- return *this;
- }
- ~Class() { if(p) delete[] p; }
- };
- void debug(const Class &x) {
- std::cout << "i: " << x.geti() << std::endl;
- std::cout << "p: ";
- x.addr();
- }
- int main() {
- Class a(50);
- debug(a);
- std::cout << std::endl;
- a = (Class(423));
- debug(a);
- std::cout << std::endl;
- Class b(a);
- b.increment();
- a = b;
- debug(a);
- std::cout << std::endl;
- Class c(std::move(Class(121)));
- debug(c);
- #ifdef _WIN32
- system("pause");
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement