首页 > 代码库 > C++提前delete

C++提前delete

/////////////////////////////////////////类析构以后,成员变量内存空间释放,///函数 和 变量 还是可以引用的//////////////////////////////////////#include <iostream>using namespace std;class CTest{public:    CTest();    ~CTest();    void Print() { cout<<"....."<<m_refCount<<endl; }    void AddRef();    void ReleaseRef();    int GetRef() { return m_refCount; }    void destroy();private:    int        m_refCount;};CTest::CTest(){    m_refCount = 1;}CTest::~CTest(){}void CTest::AddRef(){    ++m_refCount;    Print();}void CTest::ReleaseRef(){    --m_refCount;    Print();    if(m_refCount <= 0)    {        delete this;        //return;        Print();    }}void CTest::destroy(){    delete this;}int main(){    CTest* ct = new CTest;    //一定要声明为指针,不然delete时会报错。    /*for(int _i = 0; _i < 5; ++_i)    {        ct->AddRef();    }    for(int _i = 0; _i < 6; ++_i)    {        ct->ReleaseRef();    }*/    delete ct;    ct->Print();    ct->AddRef();    ct->Print();    //system("pause");    return 0;}

 

C++提前delete