Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Spartacus

How to report when an constructor fails???

This topic is 5792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Real programmers don''t document, if it was hard to write it should be hard to understand

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
Correct me if I''m wrong, but throwing exceptions from a constructor might (and in some cases definetly will) result in a memory leak.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

    
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]

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
or use constructor-try blocks (which MSVC doesn''t support).

Sure it does, at least MSVC.NET.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!