- Replace Pass-by-value with Pass-by-reference-to-const as much as possible. The former is usually more efficient because it avoids the call to copy constructors and destructors, and avoids cutting problems.
is a good example of slicing problems:
Class A {public:a () {}virtual ~a () {}virtual void Display () {cout << "A::D isplay ()" << Endl;}; Class B:public A{public:b () {}virtual ~b () {}virtual void Display () {cout << "b:display ()" << Endl;}; void Show (A a) {A.display ();} int _tmain (int argc, _tchar* argv[]) {b b; Show (b); return 0;}
Output:
2. If you spy on the bottom of the C + + compiler, you will find that references is often implemented as pointers, so pass-by-reference often means that pointers are really passed, so the above rules do not apply to built-in types, as well as STL iterators and function objects. For them, pass-by-value tend to be more appropriate.
<<effective c++>> Reading notes---clause 20: better to replace Pass-by-value with Pass-by-reference-to-const