3. Using the C++ Compiler Options
6. Creating and Using Templates
9. Improving Program Performance
9.1 Avoiding Temporary Objects
10. Building Multithreaded Programs
12. Using The C++ Standard Library
13. Using the Classic iostream Library
C++ classes, including structures and unions, are passed and returned by value. For Plain-Old-Data (POD) classes, the C++ compiler is required to pass the struct as would the C compiler. Objects of these classes are passed directly. For objects of classes with user-defined copy constructors, the compiler is effectively required to construct a copy of the object, pass a pointer to the copy, and destruct the copy after the return. Objects of these classes are passed indirectly. For classes that fall between these two requirements, the compiler can choose. However, this choice affects binary compatibility, so the compiler must choose consistently for every class.
For most compilers, passing objects directly can result in faster execution. This execution improvement is particularly noticeable with small value classes, such as complex numbers or probability values. You can sometimes improve program efficiency by designing classes that are more likely to be passed directly than indirectly.
In compatibility mode (-compat[=4]), a class is passed indirectly if it has any one of the following:
A user-defined constructor
A virtual function
A virtual base class
A base that is passed indirectly
A non-static data member that is passed indirectly
Otherwise, the class is passed directly.
In standard mode (the default mode), a class is passed indirectly if it has any one of the following:
A user-defined copy constructor
A user-defined destructor
A base that is passed indirectly
A non-static data member that is passed indirectly
Otherwise, the class is passed directly.
To maximize the chance that a class will be passed directly:
Use default constructors, especially the default copy constructor, where possible.
Use the default destructor where possible. The default destructor is not virtual, therefore a class with a default destructor should generally not be a base class.
Avoid virtual functions and virtual bases.
Classes (and unions) that are passed directly by the C++ compiler are passed exactly as the C compiler would pass a struct (or union). However, C++ structs and unions are passed differently on different architectures.
Table 9-1 Passing of Structs and Unions by Architecture
|