#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <memory>
#include <map>
#include <set>

class HasPtr
{
public:
	HasPtr(std::string const &s = std::string())
		: ptr(new std::string(s)), use(new std::size_t(1)) {}
	HasPtr(const HasPtr &p)
		: ptr(p.ptr), use(p.use)
	{
		++*use;
		std::cout << "copy constructor" << std::endl;
	}
	~HasPtr()
	{
		if (--*use == 0)
		{
			delete ptr;
			delete use;
		}
	}
	HasPtr& operator=(const HasPtr &rhs)
	{
		//++*rhs.use;
		if (--*use)
		{
			delete ptr;
			delete use;
		}
		++*rhs.use;
		ptr = rhs.ptr;
		use = rhs.use;

		std::cout << "operaator=" << std::endl;

		return *this;
	}
private:
	std::string *ptr;
	std::size_t *use;
};

void f(HasPtr x) {}

int main()
{
	HasPtr h(std::string("xxx"));
	h = h;
	f(h);
	return 0;
}