&&
and ||
short-circuit their operands
&&
second operand is not evaluated if first is false
||
second operand is not evaluated if first is true
:
) clause
bool passed = average >= 60 ? true; // compilation error -- no :
clause
cout << a[i] << (i < n-1 ? ", "); // compilation error again … no false
cout << a[i] << (i < n-1 ? ',' : ""); // compilation error true is char, false is string
?:
(which is why the above example needed ()
's)
++i
and i++
++i
— the result of the expression
is the new value of i
i = i + 1; 'return' i
i++
— the result of the expression
is the old value of i
hold = i i = i + 1; 'return' hold
int *arr = new int[n]; // n already declared and initialized
new
, one uses similar syntax:
delete [] arr;
new
will be
properly managed and eventually deleted.
new
and delete
). This can easily lead to memory leaks. Scoped variables, on the other--
e.g., local or class variables have a more manageable lifetime -- entry and exit to functions for example.
new
and delete
, control them by wrapping them in constructor/destructor
pairs