C++言語解析関数でクラスのリソースを解放

2516 ワード

//C++                

#include "stdafx.h"
#include 
#include <string>
using namespace std;

class CTest
{
private:
    char *m_Buffer;
public:
    CTest(char *text);
    ~CTest();
    void display();
};

CTest::CTest(char *text)
{
    m_Buffer = new char[255];
    strcpy(m_Buffer, text);
}
CTest::~CTest()
{
    delete [] m_Buffer;
    cout << "           ." << endl;
}
void CTest::display()
{
    cout << m_Buffer << endl;
}

int main(int argc, char * argv[])
{
    CTest test("   ,             .");
    test.display();
    return 0;
}

転載先:https://www.cnblogs.com/pythonschool/archive/2012/11/09/2762892.html