0

クラスを宣言し、クラスをインスタンス化して、それが起動することを期待しました

~CLog();

しかし、何らかの理由で、そうではありません。なぜこれが起こるのか、明らかなエラーを見た人はいますか?

終了するボイド内でクラスを宣言したので、発火する必要があると思います。クラスを明示的に破棄するわけではありませんが、自動的にスコープから外れて終了することを期待していました。

#pragma once
#include <fstream>
using namespace std;

class CLog 
{
  public:
    CLog(wstring filename);
    ~CLog();
    void WriteString(wstring uString);
  private:
    wofstream m_stream;
    wstring m_sPath;
};

#include "log.h";
#include "strhelper.h"

#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

wstring m_sText=L"";
wstring m_sPath=L"";

CLog::CLog(wstring uPath) 
{
    m_sPath=uPath;
}
void CLog::WriteString(wstring uString)
{
    m_sText+=uString;
    m_sText+=L"\n";
}
CLog::~CLog()
{

    if (FileExists(m_sPath))
    {
        DeleteFile(m_sPath);
    }

    //open for appending
    m_stream.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff,std::generate_header>));
    m_stream.open(m_sPath,fstream::in | fstream::out | fstream::app); 

    m_stream << m_sText.c_str();

    m_stream.close();
}

私はClogこのように使用しています

void foo() {
  wstring sLogPath;
  sLogPath=GetSpecialFolderDesktop() + L"\\load.log";
  CLog *pLog = new CLog(sLogPath);
  pLog->WriteString(L"Something);
}

VC2010を使用しています。

4

1 に答える 1

4

を動的にインスタンス化していますCLog。その場合は、明示的に削除する必要があります。stackClog log(sLogPath)に作成すると、オブジェクトがスコープ外になったときにデストラクタが呼び出されます。

于 2013-10-02T13:34:07.167 に答える