Tip 1: <iostream.h> or <iostream>? Many C++ programmers still use <iostream.

h> instead of the newer, standard compliant <iostream> library. What are the differences between the two? First, the .h notation of standard header files was deprecated more than five years ago. Using deprecated features in new code is never a good idea. In terms of functionality, <iostream> contains a set of templatized I/O classes which support both narrow and wide characters, as opposed to <iostream.h> which only supports char-oriented streams. Third, the C++ standard specification of iostream's interface was changed in many subtle aspects. Consequently, the interfaces and implementation of <iostream> differ from those of <iostream.h>. Finally, <iostream> components are declared in namespace std whereas <iostream.h> components are global.

Because of these substantial differences, you cannot mix the two libraries in one program. As a rule, use <iostream> unless you're dealing with legacy code that is only compatible with <iostream.h>.

Tip 2: Binding a Reference to an Rvalue Rvalues and lvalues are a fundamental concept of C++ programming. In essence, an rvalue is an _expression that cannot appear on the left-hand side of an assignment _expression. By contrast, an lvalue refers to an object (in its wider sense), or a chunk of memory, to which you can write a value. References can be bound to both rvalues and lvalues. However, due to the language's restrictions regarding rvalues, you have to be aware of the restrictions on binding references to rvalues, too.

Binding a reference to an rvalue is allowed as long as the reference is bound to a const type. The rationale behind this rule is straightforward: you can't change an rvalue, and only a reference to const ensures that the program doesn't modify an rvalue through its reference. In the following example, the function f() takes a reference to const int:

void f(const int & i); int main() {

let's see what a comma separated _expression is. the function f() could have modified its argument. void f(const A& a).f(2). they are destroyed immediately afterwards.and while-loops rather often. Yet. Note that had we declared the reference i without the const qualifier. binding a temporary A to a const reference*/ } Tip 3: Comma-Separated Expressions Comma-separated expressions were inherited from C. The temporary and its reference exist from the moment f() is invoked until it returns. It's likely that you use such expressions in for. First. thereby causing undefined behavior. You may bind a reference to a temporary object only if it's const: struct A{}. int main() { f(A()). C++ creates a temporary object of type int with the value 2 and binds it to the reference i. you may only bind references to const objects. For this reason. /* OK. At runtime. /* OK */ } The program passes the rvalue 2 as an argument to f(). An _expression may consist of one or more sub-expressions separated by commas. For example: . The same rule applies to user-defined objects. the language rules in this regard are far from being intuitive.

good() returns true. while( ++i.repeat as long as j is not 0*/ } Tip 4: Calling a Function Before Program's Startup Certain applications need to invoke startup functions that run before the main program starts. the if condition above evaluates as true only if cin. The easiest way to achieve this is by calling these functions from a constructor of a global object. billing. Because global objects are conceptually constructed before the program's outset. and logger functions must be invoked before the actual program begins. the value of an entire commaseparated _expression is only the result of the rightmost _expression. For example: class Logger { public: Logger() . int i=0. cin. However.if(++x. C++ ensures that each of the expressions is evaluated and its side effects take place.. Therefore. --y.good()) /*three expressions*/ The if condition contains three expressions separated by commas. these functions will run before main() starts. For example. Here's another example of a comma _expression: int j=10. polling. --j) { /*.

//.. it can read data from the log file. application code } The global object log is constructed before main() starts. . Thus. During its construction. log invokes the function activate_log().{ activate_log(). } }. Logger log. when main() starts. /*global instance*/ int main() { record * prec=read_log().