Error Handling in Constructor

This is a discussion on Error Handling in Constructor within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Bubba I rarely do allocation or anything to do with any resource in a constructor. ... So ...

  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,317
    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,317
    Still easier than managing the memory yourself, especially once you've used the tools a few times and you are more familiar with them.

Page 3 of 3 FirstFirst 123
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-25-2006, 11:59 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21