• Articles
  • Alternative Construction Methods
Jun 18, 2011 (last update: Jun 18, 2011)

Alternative Construction Methods

Score: 2.5/5 (38 votes)
*****
Most people complain about exceptions. They then proceed to complain that exceptions are the only way to error check if an object has failed, giving them the feeling of it being forced on them. It may be the best "best" but it's definitely not the only way. One of the most popular methods around this is two step construction.

1
2
3
4
5
6
class Example
{
        Example();
        bool Create();
        bool bOkay;
};


I personally think it's counter intuitive. No matter though, it's applied in places where we may even want to use exceptions such as company code. You'll often have questions such as, "Is it beneficial in any other way?" or "Is there any time I'll want to do this over one step construction?". The answer to both is I don't know. Create does not allow for the optimization of a ctor initialization list where the ctor itself does. It always has basic function overhead since more calls are being made. One could argue that the overhead exceptions causes is greater than what is caused by a two step constructor. This is a valid argument. Exceptions are really heavy when it comes to throwing them. However, it's my belief that the benefits of exceptions outweigh the costs. I wouldn't actually know, I've never cared to count the cycles or benchmark it.
I know and it's awesome. In C though, there's often macros that wrap around API functions like "APICHECK(myAPIFunction(structBlahblahblah));" This is done with OpenGL inside of SFML and various other areas in C.