Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualÁlvaro

Posted 12 January 2013 - 09:53 PM

I think I disagree with some of your pros:
Two-Phase:
Pros:
  • Can control initialization and cleanup of object outside allocating space for the object. So if I wanted an array of objects that initialize with certain variables it doesn't get annoying if objects don't have default constructors.
  • Can have a return result for success or failure. Little simpler than throwing an exception & catching it outside object construction.
  • Can call virtual functions within initialization.
 


I'll tell you how I would achieve those things without the two-phase construction. In order:
  • You can allocate the space without calling the constructor. This is a little bit tricky, but std::vector does it for you in the vast majority of situations where you might need to do this (you can call .reserve on a vector to allocate the space, but the object won't be constructed until you do something like calling .push_back).
  • You can set an internal flag to indicate bad status if the construction failed. You can see an example in std::ifstream, where you can use a constructor that tries to open a file, and then you can check if the opening succeeded or not with .good(). I don't believe most classes will need to do this.
  • I have never had a need to call virtual functions from a constructor. Then again, I don't use inheritance a whole lot.
But the main problem with two-phase construction is that you can't really take advantage of RAII, which is a wonderful feature of well-written C++.

#1Álvaro

Posted 12 January 2013 - 09:51 PM

I think I disagree with some of your pros:
Two-Phase:
Pros:
  • Can control initialization and cleanup of object outside allocating space for the object. So if I wanted an array of objects that initialize with certain variables it doesn't get annoying if objects don't have default constructors.
  • Can have a return result for success or failure. Little simpler than throwing an exception & catching it outside object construction.
  • Can call virtual functions within initialization.


I'll tell you how I would achieve those things without the two-phase construction. In order:
  • You can allocate the space without calling the constructor. This is a little bit tricky, but std::vector does it for you in the vast majority of situations where you might need to do this (you can call .reserve on a vector to allocate the space, but the object won't be constructed until you do something like calling .push_back).
  • You can set an internal flag to indicate bad status if the construction failed. You can see an example in std::ifstream, where you can use a constructor that tries to open a file, and then you can check if the opening succeeded or not with .good(). I don't believe most classes will need to do this.
  • I have never had a need to call virtual functions from a constructor. Then again, I don't use inheritance a whole lot.

    But the main problem with two-phase construction is that you can't really take advantage of RAII, which is a wonderful feature of well-written C++.


PARTNERS