C++自删除

#pragma once
class AutoRelease
{
public:
	AutoRelease(void){ m_count = 0; }
	virtual ~AutoRelease(void){}
	AutoRelease* GetPointClone()
	{
		++m_count; 
		return this; 
	}
	void Release()
	{ 
		m_count = m_count - 1;  
		if( m_count <= 0) 
			delete this; 
	}

private:
	long m_count;
};


#include <iostream>
#include "AutoRelease.h"

using namespace std;

class A:public AutoRelease
{
};

void main()
{
	A* pa = (A*)((new A())->GetPointClone());
	pa->Release();
	system("pause");
}




你可能感兴趣的:(C++自删除)