keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
Thanks, I can do that.
Just out of curiosity, is there any way to write this with more of a "RAII"-ish behavior, without having to split acquisition and initialization up?
a_definition is indeed out of scope at teh place you mention, but so what ?! You are anyway using it inside the try-catch block (I hope) so you anyway don't it outside the try catch, so let it go out of scope. Now if you really want a code that gives a fake feeling of cleanliness you can do this:
Your second example is even worse because the object will go out of scope as soon as do_that_thing() returns, so any usage of it inside the try ... catch block will now be illegal.
Back on topic:
If you really need to use code outside the constructor, use pointers, although it is a bad design if you are forced to use an object out of its try catch block.
Your second example is even worse because the object will go out of scope as soon as do_that_thing() returns, so any usage of it inside the try ... catch block will now be illegal.
May be I wasn't clear. What was meant is that create the object and do whatever you wanna do with the object "inside" do_that_thing_with_a_definition(). I thot the name of the function will make it clear. Anyway..
May be I wasn't clear. What was meant is that create the object and do whatever you wanna do with the object "inside" do_that_thing_with_a_definition(). I thot the name of the function will make it clear. Anyway..
You basically implied that a_definition would be in scope out of the try...catch if you used do_that_thing() function. However, that's not the case. It suffers the same fate as the previous problem, so putting it in a function doesn't really do much good. In fact, you've effectively reduced the scope of the object by allocating it in a function.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.