How can I conditionally instantiate an object?
问题 I'm trying to do some conditional work like so: Type object; if (cond) { doSomeStuff(); object = getObject(); doMoreStuff(); } else { doSomeOtherStuff(); object = getDifferentObject(); doEvenMoreStuff(); } use(object); The only way I can think of solving this is the duplicate the use code (which is actually inline code in my application) and declare object in each branch of the if block. If I wanted to avoid duplicate code I'd have to wrap it in some use function, as I have above. In a real