I will not reply to this one since Sef already provided more than enough information of the drawbacks of these solutions...Quote:
Originally posted by AnthonyMai
OK, let's talk about constructors that would fail. First, you CAN write constructors that would never fail unless catastrophy happens (like some one pulling the power plug or some memory gets over-written). You can postpone resource acquisition to later, after you have constructed the object. Further, even if the constructor can fail, you can report the error using a boolean within the object or even a static variable to indicate the error condition. There is no need to throw an exception.
First of all...I could reply:Quote:
Originally posted by AnthonyMai
Let's step back one step, let's say your constructor fails, and you do throw an exception. The question is WHO WILL catch the exception? Let's say you have a global instance of that object. Upon program loading, the constructor of that object gets called and it throws an exception. But it hasn't even started the main() function yet. So even if you have a big try/catch in the main(), you will still not be able to catch the exception!!!
Let's say a constructor sets a boolean flag inside the class. The question is WHO WILL check this flag?
Second thing...global instances. There is nearly no need to define global instances in an object-oriented design...nevertheless I agree that this case would be inappropriate but that is one typical thing of your discussions...just because one case does not work - DOES NOT - mean that the whole thing is bad in general...
Sorry...but this clearly shows that you did not understand the concept...Quote:
Originally posted by AnthonyMai
I know about the RAII concept. Some one likes it. But I think it is a BAD idea. It mixes three things together: 1.Construction of an object. 2.Acquire resources that the object will be using. 3.Initialize the resource acquired. RAII attempts to do all three things at once.
The reality is you rarely want the three things to happen spontaneously. You may want to construct an object but postpone resource acquisition until when it is actually used.
You may re-acquire new resources or release old ones during the lifetime of an object, without destorying and reconstructing the object. Also, many times you do not want to initialize resource right upon acquisition, or simply is unable to initialize it yet at the moment of acquisition. So, object construction, resource acquisition, resource initialization. These three things rarely happen at the same time. RAII is an ill-formed idea that doesn't fit in the real world.
Anyway...I knew that this would happen and I will stop right here. We are already off-topic and I will not be part of another one of your endless discussions...
