Programming C++  «Prev 

C++ const and type safety

The use of const is highly desirable because it protects a variable from inadvertent change and documents the use of the value. Furthermore, the compiler is at liberty to optimize code connected to constants, for example storing them in read-only high-speed memory.
Type-safety is an important property of C++. Compilers are able to perform type checking statically and consequently find bugs that would be difficult to discover at runtime. The narrower the type, the more safety and, frequently, the more efficiency. Integer arithmetic is usually more accurate and faster than floating-point arithmetic.
A char or a short takes less space than an int or a long.

Implicit type conversions

When an expression contains operands of different built-in types, and no explicit casts are present, the compiler uses built-in standard conversions to convert one of the operands so that the types match. The compiler tries the conversions in a well-defined sequence until one succeeds. If the selected conversion is a promotion, the compiler does not issue a warning. If the conversion is a narrowing, the compiler issues a warning about possible data loss. Whether actual data loss occurs depends on the actual values involved, but we recommend that you treat this warning as an error. If a user-defined type is involved, then the compiler tries to use the conversions that you have specified in the class definition. If it cannot find an acceptable conversion, the compiler issues an error and does not compile the program.

Constants

In pre-Standard C, if you wanted to make a constant, you had to use the preprocessor:
#define PI 3.14159
Everywhere you used PI, the value 3.14159 was substituted by the preprocessor (you can still use this method in C and C++). When you use the preprocessor to create constants, you place control of those constants outside the scope of the compiler. No type checking is performed on the name PI and you cannot take the address of PI (so you cannot pass a pointer or a reference to PI). PI cannot be a variable of a user-defined type. The meaning of PI lasts from the point it is defined to the end of the file; the preprocessor does not recognize scoping.
C++ introduces the concept of a named constant that is just like a variable, except that its value cannot be changed. The modifier const tells the compiler that a name represents a constant. Any data type, built-in or user-defined, may be defined as const. If you define something as const and then attempt to modify it, the compiler will generate an error. You must specify the type of a const, like this:
const int x = 10;
In Standard C and C++, you can use a named constant in an argument list, even if the argument it fills is a pointer or a reference (for example, you can take the address of a const). A const has a scope, just like a regular variable, so you can hide a const inside a function and be sure that the name will not affect the rest of the program.
The const was taken from C++ and incorporated into Standard C, albeit quite differently. In C, the compiler treats a const just like a variable that has a special tag attached that says "Do not change me." When you define a const in C, the compiler creates storage for it, so if you define more than one const with the same name in two different files (or put the definition in a header file), the linker will generate error messages about conflicts. The intended use of const in C is quite different from its intended use in C++.