Thread: Error Handling in Constructor

  1. #31
    Registered User
    Join Date
    Feb 2004
    Posts
    31
    Quote Originally Posted by Bubba
    I rarely do allocation or anything to do with any resource in a constructor.
    ...
    So I avoid it altogether. Constructors in most of my code simply init variables that are guaranteed NOT to fail. In other words setting pointers to NULL, numeric vars to initial values, etc, etc. This may not be the best approach, but it helps me out a great deal. The others here might have better solutions.
    So you would go for the second or third example presented in my original post?
    Parts of my days are spent bug fixing...err. I’m sorry...I’ve just been reminded that we don’t have bugs. We have undocumented features. (Jonathan Ackley on Monkey Island 3)

  2. #32
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Question

    Quote Originally Posted by Daved
    Also, in C++ you rarely actually need to do allocations directly in the constructors of your objects. You should be using tools that handle memory management for you (like string or vector). In those cases you will be forced to use exceptions for memory issues unless you specifically force new to not throw an exception when it fails.
    I have a follow-up question regarding this:

    How do you make, say, a vector handle memory management if you are allocating your objects on the heap which you would probably do to avoid copying your objects everytime the vector is expanded?
    Parts of my days are spent bug fixing...err. I’m sorry...I’ve just been reminded that we don’t have bugs. We have undocumented features. (Jonathan Ackley on Monkey Island 3)

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you are storing pointers in your vector (usually because your classes are expensive to copy or because you are using polymorphism), then you cannot make vector manage that memory.

    Instead, you use shared_ptr to manage the memory of the objects while vector manages the array memory that holds the pointers. Another option is to use a ptr_vector. Both shared_ptr and ptr_vector are in boost. The ptr_vector class is probably a better fit than a vector of shared_ptr's, but the shared_ptr is almost standard and will be part of the next standard, so if you start using it now it is more likely to always be available in the future.

    In the case of shared_ptr, you still call new yourself, but the effect is the same because it will clean up automatically and any out of memory exceptions will propagate out of the constructor just as they would if they came from the vector.

  4. #34
    Registered User
    Join Date
    Feb 2004
    Posts
    31
    I see. That makes more sense.

    So much to do to avoid memory leaks
    Parts of my days are spent bug fixing...err. I’m sorry...I’ve just been reminded that we don’t have bugs. We have undocumented features. (Jonathan Ackley on Monkey Island 3)

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Still easier than managing the memory yourself, especially once you've used the tools a few times and you are more familiar with them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM