Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Thursday, January 21, 2010

C++ Order of evaluation


Better not to assume the order of evaluation in a C++ subexpressions. Also do not use ++ and -- operators as part of subexpressions (Eg: n + (++n))

Guaranteed order: Subexpressions using AND(&&) OR(||) and COMMA(,) operators are always evaluated in left-to-right manner. Helps with "Short-circuit evaluation" for relational operators!

General order:
Unary operator: right-to-left
Assignment: right-to-left Eg: a = b = c; OR a = (b = c);
Binary operator: left-to-right


Back to Top


Typecasting in C++


static_cast<type>(Expression) 
dynamic_cast<type>(Expression) 
const_cast<type>(Expression) 
reinterpret_cast<type>(Expression)  

static cast alternatives (not recommended): 
(type) Expression 
type(Expression)  
Eg: static_cast<double>(3) || double(3) || (double) 3
Back to Top


 

Labels