#include <iostream>
#include <vector>
using namespace std;

class matrix_rc
{
public:
	matrix_rc()
	{
		inst = new instance;
	}

	matrix_rc(const matrix_rc& other)
	{
		cout << "Лёгкое копирование matrix_rc ("
			<< (other.inst
				? "refcount: " + to_string(other.inst->refcount) + " -> " + to_string(other.inst->refcount + 1)
				: "пустой")
			<< ")." << endl;

		if (other.inst) other.inst->refcount++;
		this->inst = other.inst;
	}

	~matrix_rc()
	{
		cout << "Лёгкое уничтожение matrix_rc ("
			<< (inst
				? "refcount: " + to_string(inst->refcount) + " -> " + to_string(inst->refcount - 1)
				: "пустой")
			<< ")." << endl;

		if (inst && --inst->refcount == 0) delete this->inst;
	}

private:
	class instance
	{
		friend class matrix_rc;
		instance()
		{
			cout << "Тяжёлое создание matrix_rc::instance (refcount: 1)." << endl;
			data = new double[1000 * 1000];
			refcount = 1;
		
		}

		~instance()
		{
			cout << "Тяжёлое уничтожение matrix_rc::instance." << endl;
			delete[] data;
		}

		double* data;
		size_t refcount;
	};

	instance* inst;
};

int main()
{
	vector<matrix_rc> v;
	v.push_back(matrix_rc());
	return 0;
}