= != =

#include <iostream>

using std::cout;
using std::move;
class Book {
       public:
	Book() { cout << "Default ctor → Book::Book()\n"; }
	~Book() { cout << "Default dtor → Book::~Book()\n"; }

	Book(const Book&) {
		cout << "Copy ctor → Book::Book(const Book& rhs)\n";
	}
	Book& operator=(const Book& rhs) {
		cout << "Copy operator= → Book& Book::operator=(const Book& rhs)\n";
		return *this;
	}

	Book(const Book&&) {
		cout << "Move ctor → Book::Book(const Book&& rhs)\n";
	}
	Book& operator=(const Book&& rhs) {
		cout << "Move operator= → Book& Book::operator=(const Book&& rhs)\n";
		return *this;
	}
};
Book one;
Output:
Default ctor  Book::Book()
Default dtor  Book::~Book()

Book one;
Book two = one;
Output:
Default ctor  Book::Book()
Copy ctor  Book::Book(const Book& rhs)
Default dtor  Book::~Book()
Default dtor  Book::~Book()

Book one, two;
two = one;
Output:
Default ctor  Book::Book()
Default ctor  Book::Book()
Copy operator=  Book& Book::operator=(const Book& rhs)
Default dtor  Book::~Book()
Default dtor  Book::~Book()

Book one;
Book two = move(one);
Output:
Default ctor  Book::Book()
Move ctor  Book::Book(const Book&& rhs)
Default dtor  Book::~Book()
Default dtor  Book::~Book()

Book one, two;
two = move(one);
Output:
Default ctor  Book::Book()
Default ctor  Book::Book()
Move operator=  Book& Book::operator=(const Book&& rhs)
Default dtor  Book::~Book()
Default dtor  Book::~Book()

C++

207 Words

2018-09-05 03:00 +0300