Synopsis: | Use constant references (const &) instead of call-by-value, unless using a basic data type, a simple object or a pointer |
Language: | C++ |
Severity Level: | 7 |
Category: | Class Interface |
Description: |
Method/function arguments are invoked according to call-by-value. This means that values are copied with invocations to constructors and destructors as a result. This does no harm for basic data types, simple objects and pointers, but reduces performance for complex objects. When arguments are passed using references (call-by-reference), only the reference itself is copied. A simple object is defined as an object that has at most 5 leave basic data types. E.g. bothvoid SetComplexObject(const ComplexObject sName); // Less efficient. void SetComplexObject(const ComplexObject& rsName); // More efficient. struct Coordinate { int x; int y; int z; }; and class Product { ... private: Coordinate loc; float price; }; are considered simple objects in this context. |