#include <iostream>
using namespace std;
class MyClass;
namespace std {
std::ostream& operator<<(std::ostream& os, MyClass const& s);
}
class MyClass {
public:
~MyClass() {
m_state = "d-ted";
std::cout << "S d-tor " << this << std::endl;
}
MyClass() : m_state("dflt c-ted") {
std::cout << "S dflt c-tor " << this << std::endl;
}
MyClass(MyClass const& o) : m_state("copied") {
std::cout << "S copy c-tor " << o << " -> " << *this << std::endl;
}
MyClass(MyClass&& o) noexcept : m_state("move c-ted") {
o.m_state = "moved_out";
std::cout << "S move c-tor " << o << " -> " << *this << std::endl;
}
MyClass& operator=(MyClass o) {
std::cout << "operator= " << o << " -> " << *this << std::endl;
swap(*this, o);
m_state = "assigned";
return *this;
}
friend void swap(MyClass& l, MyClass& r) {
std::cout << "swap " << l << " <-> " << r << std::endl;
l.m_state = r.m_state = "swapped";
}
std::string const & state() const {
return m_state;
}
std::string & get_state() {
return m_state;
}
private:
std::string m_state;
};
namespace std {
std::ostream& operator<<(std::ostream& os, MyClass const& s) {
return os << &s << " " << s.state() << "\n";
}
}
//S my_global_s;
bool volatile cosmic_radiation = true;
MyClass foo(MyClass mys) {
std::cout << "in foo " << mys << std::endl;
mys.get_state() = "manipulated";
if(cosmic_radiation) {
mys.get_state()[4] = 'F';
}
return mys;
}
int main() {
//S first; // dflt c-tor
//S second(first); // copy c-tor
//S third(std::move(first)); // move c-tor
//first = third; // copy - op= - swap - dtor
//first = std::move(third); // move - op= - swap - dtor
//swap(first, second); // swap
auto s = foo(MyClass());
std::cout << "in main " << s << std::endl;
std::cout << s.get_state() << std::endl;
return 0;
// dtor dtor dtor
}