Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef A_H
- #define A_H
- #include <iostream>
- class A {
- int* a;
- public:
- A() : a(NULL) {
- std::cout << "Called Default Constructor" << std::endl;
- a = new int(10);
- }
- ~A() {
- std::cout << "Called Destructor" << std::endl;
- delete a;
- }
- A(const A& other) : a(NULL) {
- std::cout << "Called Copy Constructor" << std::endl;
- a = new int(*(other.a));
- }
- A(A&& old) : a(NULL) {
- std::cout << "Called Move Constructor" << std::endl;
- a = old.a;
- old.a = NULL;
- }
- A& operator=(const A& other) {
- // Self Assignment:
- // int a = rand();
- // a = a; // This is self assignment
- if(this != &other) {
- std::cout << "Called Copy Assignment" << std::endl;
- delete a;
- a = new int(*(other.a));
- }
- return *this; // This is standard
- // int a = rand();
- // int b, c;
- // c = b = a;
- }
- A& operator=(A&& old) {
- if(this != &old) {
- std::cout << "Called Move Assignment" << std::endl;
- delete a;
- a = old.a;
- old.a = NULL;
- }
- return *this;
- }
- friend std::ostream& operator<<(std::ostream& os, const A& obj);
- };
- std::ostream& operator<<(std::ostream& os, const A& obj) {
- return os << obj.a << " " << *(obj.a);
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement