   C++ constructor question
  Hey guys.  Sometimes when looking at C++ source code,  I run into the following,if(CMammal *pMammal = new CMammal()){pMammal->speak();}In this case what does the if condition evaluate exactly to determine that the statement is true?  Is it evaluating the value of the pMammal pointer?  Also,  how could I make this if conditional fail?Finally, is this a good coding practice?  Should this type of code be avoided?  If so,  when might you decide to use it?  Thanks!   
Quote:Is it evaluating the value of the pMammal pointer?Yes - I think the idea behind this code is that the block of code following the conditional is only executed if the memory is successfully allocated.However, IIRC, new throws if it can't allocate memory; it doesn't return a null pointer (unless you use the 'no throw' version). More info here.So (unless I'm missing something) there's really no point to the conditional in your example. It should really just be:CMammal *pMammal = new CMammal;pMammal->speak();;  Yup. The code in question is likely either pre-standard C++ or written by somebody who doesn't know how new works (or on a broken implementation of the language, perhaps, or one where exceptions were disabled and new thus returns null, which is implementation-specific).From the perspective of the C++ language, that if() is pretty useless. In fact, it also leaks memory, since the scope of pMammal is limited to the if block itself, where it is never delete'd.   ;  jyk is correct.As for your question whether it is good practice. Well, generally this is a form that is avoided. Assignments are best placed outside the if-statement or any other statement that has conditionals. You're already one example why it should be avoided -- it confuses people.   ;  OT, I know, it's just that this code reminded me the using statement in c# :-)   ;  Thanks jyk.  That makes a lot of sense.  I still am not quite sure what the if conditional is using as a basis to determine that the statement is true.  Is it evaluating the pMammal variable to determine that the the statement is true or false?Also,  do you guys find yourself using the nothrow often in your own code?   ;  I hate that C++ even lets you do this. Not only its confusing, its not obvious that the scope is just inside the brackets. And because of that, as pointed outthere is a memory leak.   ;Quote:I still am not quite sure what the if conditional is using as a basis to determine that the statement is true. Is it evaluating the pMammal variable to determine that the the statement is true or false?Yes. It's a pointer, and in a boolean context pointers are converted to 'true' if non-null, false otherwise. Quote:Also, do you guys find yourself using the nothrow often in your own code?Almost never. I much prefer exceptions as an error propagation mechanism, when appropriate.   ;  Thanks for the tips Josh!  =)   ;Quote:Original post by ConcentrateI hate that C++ even lets you do this.It can be quite useful. Contrived example:if (Player* p = dynamic_cast<Player*>(unit)){    p->doPlayerStuff();}