Half baked object when using inheritance in C++ SDK

Half baked object when using inheritance in C++ SDK

I used the recommended inheritance model for an Application class. I get a half baked object from a derived class (SampleApplication). class SampleApplication : public Application { } The reason is that the Application::ctor throws an exception before the ctor of the derived class is called to create its object. Therefore, SampleApplication is not yet created. The root cause: Application object is not completely created. Solution: 1) Do not call ADP_Initialize() and ADP_isAuthorized() functions in the Application::ctor(). 2) Add a Initialize() method in Application() and call these two functions. This method also throws the needed exceptions. Here is the sample code I have. Header file: #pragma once #include "adpcppf.h" class SampleApplication : public Application { public: SampleApplication(const wchar_t* pszData, ApplicationId appId); virtual ~SampleApplication(void); private: wchar_t* m_szData; ApplicationId m_appId; }; Implementation file: #include "SampleApplication.h" SampleApplication::SampleApplication(const wchar_t* pszData, ApplicationId appId) : Application(appId) { if (NULL == pszData) { return; } m_szData = NULL; m_appId = appId; size_t ulLen = wcslen(pszData); m_szData = new wchar_t[ulLen + 1]; if (NULL != m_szData) { memset(m_szData, 0, ulLen+1); wcsncpy_s(m_szData, ulLen, pszData, ulLen); } } SampleApplication::~SampleApplication(void) { // intentionally not checking for null to show intended effects of using inheritance from Application delete [] m_szData; } Main function: #include #include "SampleApplication.h" #include "AdpRuntimeException.h" #include "InitializationException.h" #include "UnauthorizedException.h" int _tmain(int argc, _TCHAR* argv[]) { ApplicationId appId = ApplicationId(0x00000000, 0x12129090, 0xF0F0ABAB, 0x00006262); SampleApplication *pApp = NULL; try { pApp = new SampleApplication(L"Don't corrupt my memory!", appId); } catch (AdpRuntimeException ex) { wcout << "Runtime error occured: " << ex.GetCode() << endl; } catch (InitializationException ex) { wcout << "Initialization error occured: " << ex.GetCode() << endl; } catch (UnauthorizedException ex) { wcout << "Authorization error occured: " << ex.GetCode() << endl; } catch (AdpException ex) { wcout << "An ADP error occured: " << ex.GetCode() << endl; } if (NULL != pApp) { wcout << "This application is authorized to run." << endl; delete pApp; } return 0; }
1 post / novo 0
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.