コンストラクターに次のコードを入れて、XML ドキュメントをメンバー変数にロードしても問題ありませんか? 問題がある場合は、呼び出し元にスローします。
MSXML2::IXMLDOMDocumentPtr m_docPtr; //member
Configuration()
{
try
{
HRESULT hr = m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument40));
if ( SUCCEEDED(hr))
{
m_docPtr->loadXML(CreateXML());
}
else
{
throw MyException("Could not create instance of Dom");
}
}
catch(...)
{
LogError("Exception when loading xml");
throw;
}
}
より効果的な C++ での Scott Myers RAII 実装に基づいて、リソース、つまりポインターを割り当てる場合、彼はクリーンアップします。
BookEntry::BookEntry(const string& name,
const string& address,
const string& imageFileName,
const string& audioClipFileName)
: theName(name), theAddress(address),
theImage(0), theAudioClip(0)
{
try { // this try block is new
if (imageFileName != "") {
theImage = new Image(imageFileName);
}
if (audioClipFileName != "") {
theAudioClip = new AudioClip(audioClipFileName);
}
}
catch (...) { // catch any exception
delete theImage; // perform necessary
delete theAudioClip; // cleanup actions
throw; // propagate the exception
}
}
スマート ポインター (IXMLDOMDocumentPtr) を使用しているため、CTOR から例外をスローできるようにするだけで問題ないと思います。
どう考えているか教えてください....