Real programmers don''t document, if it was hard to write it should be hard to understand
How to report when an constructor fails???
Hey!
How can I report to the calling function when the constructor of an object fails? Lets say I have a constructor like this:
CObject::CObject(const CHAR srcFile[MAX_PATH]);
and this constructor takes the path to a bmp file and loads the file. If the file does not exist, for instance, how can I notify the function constructing the object of this behaviour? Do I have to use exceptions or what?¿?
Thanks!
Spartacus
Yes, you have to use an exception.
Another horrible way to do it is for the constructor to set a flag indicating it was constructed correctly and then check the flag each time an object is created.
Another horrible way to do it is for the constructor to set a flag indicating it was constructed correctly and then check the flag each time an object is created.
I suppose exceptions could do it, or perhaps have a flag that gets set in the class that''ll tell the caller whether the object''s properly initialized or not. Still, I think the best way would to simply move any code that could fail right out of the constructor to somewhere else where it''ll be easier to test for failure.
There are three ways I can think of to do this:
1. as Melraidin said, move the initilisation out of the Constructor.
2. Melraidin''s other suggestion, set a flag and add an isValid() method.
3. Create a static method to construct the object, something like:
int clone(CObject* object, const CHAR srcFile[MAX_PATH]);
This can return an error code if anything goes wrong.
Exceptions would probably also work.
Pick whatever you like best!
Enigma
1. as Melraidin said, move the initilisation out of the Constructor.
2. Melraidin''s other suggestion, set a flag and add an isValid() method.
3. Create a static method to construct the object, something like:
int clone(CObject* object, const CHAR srcFile[MAX_PATH]);
This can return an error code if anything goes wrong.
Exceptions would probably also work.
Pick whatever you like best!
Enigma
Throw an exception. That will ensure that your partially constructed object gets cleaned up correctly. i.e. the destructors for the members that have actually been constructed will be called.
The proper place for initialisation code _is_ the constructor. "Ressource acquisition is initialisation" does wonders.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
The proper place for initialisation code _is_ the constructor. "Ressource acquisition is initialisation" does wonders.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Correct me if I''m wrong, but throwing exceptions from a constructor might (and in some cases definetly will) result in a memory leak.
It can, you have to code everything to be exception safe. Either use RAII religiously or use constructor-try blocks (which MSVC doesn''t support).
quote:Original post by kill
Correct me if I''m wrong, but throwing exceptions from a constructor might (and in some cases definetly will) result in a memory leak.
If throwing an exception in a constructor will result in a memory leak, your design is flaw.
kill:
C++ spec says that if a constructor fails (throws an exception), the memory for the object being constructed is released. You wouldn't, for instance, write this code:
In fact, if you tried running this code as-is, you might get a memory access violation because if Widget's ctor fails, the assignment to pw never happens.
[edited by - Stoffel on August 13, 2002 6:24:48 PM]
C++ spec says that if a constructor fails (throws an exception), the memory for the object being constructed is released. You wouldn't, for instance, write this code:
Widget *pw;try{ pw = new Widget;}catch (...){ delete pw; // no need for this! memory already released!}
In fact, if you tried running this code as-is, you might get a memory access violation because if Widget's ctor fails, the assignment to pw never happens.
[edited by - Stoffel on August 13, 2002 6:24:48 PM]
quote:Original post by Magmai Kai Holmlor
or use constructor-try blocks (which MSVC doesn''t support).
Sure it does, at least MSVC.NET.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement