exceptions in constructor
Hi
I've a question about constructors and exceptions.
//************************************************************
class CObject
{
public:
// ctor
CObject();
// dtor
~ CObject();
// ... methods
};
CObject::CObject()
{
// 1) if ( FAILED( LoadLibrary(...) )) throw exception1;
// 2) if ( FAILED(CoInitialize( 0 )) throw exception2;
// 3) if ( FAILED( CoCreateInstance(....))) throw exception3;
}
CObject::~CObject()
{
// 1) release COM interface
// 2) CoUninitialize();
// 2) FreeLibrary
}
int main(int avgv, char** argc)
{
try{
CObject* ptrObj = new CObject;
}
catch(exception& exc)
{...}
return 0;
}
//****************************************************************
When an error occurs in constructor there is only one way how to say
that samething wrong happen.You can throw an exception but is then a
pointer to CObject valid ?
So can you call detructor ~CObject() to release resources. I mean
this : delete ptrObj;
Is really constructor good place to attach resources (load library or
to get COM interface) ?
I'm not sure.
I've seen another way. Constructor and destructor are very simple
methods.
All resources are attached in method Initialize and released in
Uninitialize.
These methods can return error codes.
Can anybody explain it to me.
Thanks.