Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CQG / Задание1 / HDS_VCPP_Programming Rules.doc
327.68 Кб

Member Function Return Types

Rec. 38.A public member function should never return a non-const reference or pointer to member data.

Rec. 39.A public member function should never return a non-const reference or pointer to data outside an object, unless the object shares the data with other objects.

By allowing a user direct access to the protected or private member data of an object, this data may be changed in ways not intended by the class designer. This may lead to reduced confidence in the designer’s code: a situation to be avoided. A worse risk is having pointers that point to de-allocated memory. See Example 37.

Note that we do not forbid the use of protected member functions that return a const reference or pointer to member data though it is more convenient to provide direct access to those data for derived classes. See also Rec. 22.


Rec. 40.Avoid inheritance for parts-of relations.

A common mistake is to use multiple inheritance for parts-of relations (when an object consists of several other objects, these are inherited instead of using instance variables). This can result in strange class hierarchies and less flexible code. In C++ there may be an arbitrary number of instances of a given type; if inheritance is used, direct inheritance from a class may only be used once.


Rule 47.Do not attempt to create an instance of a class template or to use function template providing a type that does not define the member functions that the class or function template, according to its documentation, requires.

Rule 48.Use an explicit specialization of a class or function template for a specific type instead of deducing the template argument, if it is necessary.

Exception:If the compiler does not support explicit specifications, deduce the template argument instead.

Rec. 41.Take care to avoid multiple definition of overloaded functions in conjunction with the instantiation of a class template.

It is not possible in C++ to specify requirements for type arguments for class templates and function templates. It may imply that the type chosen by the user does not comply with the interface as required by the template. For example, a class template may require that a type argument have a comparison operator defined.

With a function or class template, you can define special behavior for a specific type by providing an explicit specialization (override) of the template. On the other hand, you can do the same using the old syntax and just deducing the template argument. Avoid the old messy syntax using the explicit specialization that is much clearer. See also Example 38andRule 49.

Another problem with type templates can arise for overloaded functions. If a function is overloaded, there may be a conflict if the element type appears explicitly in one of these. For example, two functions can have the type intas an argument. The compiler may complain about this, but there is a risk that the designer of the class does not notice it. In cases where there is a risk for multiple definition of member functions, this must be carefully documented. SeeExample 39.