If you do not want to read, the bottom line is that you need exceptions to return errors from ctors, and exceptions are bad.
As Trevor and others hinted at, there are a number of reasons for this practice. You have brought a concrete example here , so let me address this.
The textbook is dedicated to the class GraphicsClass(the name does not necessarily inspire confidence), which contains the following definitions:
class GraphicsClass
{
public:
GraphicsClass();
~GraphicsClass();
bool Initialize(int, int, HWND);
void Shutdown();
};
So it has ctor, dtor and Initialize/Shutdown. Why not condense the latter into the former? The implementation gives a few tips:
bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
bool result;
m_D3D = new D3DClass;
if(!m_D3D)
{
return false;
}
result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
if(!result)
{
MessageBox(hwnd, L"Could not initialize Direct3D", L"Error", MB_OK);
return false;
}
return true;
}
, , new D3DClass, ( , new, bad_alloc) *. , D3DClass::Initialize(). , , , - , . , ctor, .
, , : ? ++ . , , . dtor, , , . , , ++ .
; , , , C ( ctors/dtors), , A B . , , # 1 - , , .
, ++ , , , " " , .
* - ! , new D3DClass new D3DClass, , , , , , .