I don't know how to handle the error in object's constructor in c++.
Using exception?
Or just write a function initialize() to replace the constructor? (I don't like the method
it is too c style)
Please help me.
What can I do when a error occurs in Object's constructor?
According to some people, constructors should only be used to bring the object in a valid state. So, they would propably argue that the logic contained within the constructor should be so minimal that there would be virtually no errors.
In these rare occasions, throwing an exception makes perfect sense to me.
Personally I also believe in the object creation = initialisation mantra, so a separate Initialize() method would be something I'd advice against.
In these rare occasions, throwing an exception makes perfect sense to me.
Personally I also believe in the object creation = initialisation mantra, so a separate Initialize() method would be something I'd advice against.
Generally I use an Init() function that returns an error code.
A Solution used by DirectShow is to pass the constructor a reference to a return code that can be set to the return value.
ex :
CMyObject::CMyObject(HRESULT *phr)
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
A Solution used by DirectShow is to pass the constructor a reference to a return code that can be set to the return value.
ex :
CMyObject::CMyObject(HRESULT *phr)
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
You might also be interested by function-try-blocks if you want to use exceptions in constructors, see this for example:
http://www.gotw.ca/publications/mill13.htm
Hope it helps you ;)
http://www.gotw.ca/publications/mill13.htm
Hope it helps you ;)
CMyObject::CMyObject(HRESULT *phr)
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
It's pretty and simple, KISS!
Thank you all.
When a error occurs in deconstructor, what can I do?
now, my solution is to write a destroy function to return a error code,
then handle.
Is there some more better way?
Because there are too many init & destroy function in my classes!
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
It's pretty and simple, KISS!
Thank you all.
When a error occurs in deconstructor, what can I do?
now, my solution is to write a destroy function to return a error code,
then handle.
Is there some more better way?
Because there are too many init & destroy function in my classes!
Quote:Original post by songge09
CMyObject::CMyObject(HRESULT *phr)
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
It's pretty and simple, KISS!
Thank you all.
When a error occurs in deconstructor, what can I do?
now, my solution is to write a destroy function to return a error code,
then handle.
Is there some more better way?
Because there are too many init & destroy function in my classes!
Ask yourself: What would, or better, could you do in case of destructor failure?
E.g., how would you handle failure of memory de-allocation?
Quote:Original post by songge09
CMyObject::CMyObject(HRESULT *phr)
{
*phr = S_OK;
m_pBuffer = new BYTE[512];
if (!m_pBuffer)
*phr = E_OUTOFMEMORY;
}
'new' will throw an exception when it fails. You check whether m_pBuffer == NULL, which it never is because any line after 'new' can only have valid memory in this case (it bails out for an exception was thrown).
I too am against initialize() and destroy() functions, it should be handled in the constructor respectively destructor.
I do have throwing constructors sometimes, for example:
- a Texture class which takes raw pixel data, when it's not a power-of-2 I will reallocate memory to make it power-of-2 and send that to the GPU, the 'new' can throw
- a Font loader class which has to load a .ttf file, if it fails somehow, I throw (up)
Just take caution when having multiple allocations in your constructor (when a second allocation fails, deallocate the first). Also take caution when doing something like: f(new A, new B); and make sure your destructor never ever throws.
I personally would do it like this:
Sorry about the mess above but no time to format it whilst im in work. Basically putting a throw in your constructor will give an error if an object fails to initialize (picked up by the try).
[EDIT] Formatted code -twas bothering me ;-)
[Edited by - LionMX on July 2, 2010 5:39:56 AM]
//Your constructorClassOne::ClassOne(){ //Stuff if(error) { throw ErrorCode(DATA_PROBLEM) }}//GlobalClassOne *p_myClassOne = NULL;ClassTwo *p_myClassTwo = NULL;int main(void){ try { *p_myClassOne = new ClassOne; *p_myClassTwo = new ClassTwo; }}
Sorry about the mess above but no time to format it whilst im in work. Basically putting a throw in your constructor will give an error if an object fails to initialize (picked up by the try).
[EDIT] Formatted code -twas bothering me ;-)
[Edited by - LionMX on July 2, 2010 5:39:56 AM]
There's also the Null Object pattern, less affectionately known as the Zombie Pattern. But yeah, exceptions or exception-free logic are the preferred way to go, with Null objects or Init functions as last resorts. Hopefully you can name the Init function something more meaningful like Load() or something.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement