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

Classes Considerations Regarding Access Rights

Rec. 22.Limit the use ofprivatein your classes.

Rec. 23.Avoid using data members explicitly. Provide and use their access functions instead.

Though it is a common practice to avoid using public variables because of possible violation of principles of object-oriented programming, namely, encapsulation of data, you should not be purists. It is important that your users (in this case, other developers) be able to use your classes in ways that you might not have originally intended. By keeping the majority of member functions and member data public and protected, you allow for flexibility in their use.

If you are concerned about future changes to functions within your classes that might break functionality, put special comments on them. This will indicate those functions should not be relied upon to remain the same in future versions of your classes.

On the other hand, you are encouraged to reserve accessor functions for operations that do more than simply change or return a data member, such as incrementing a counter or updating another function. See Example 24.

Sometimes it becomes necessary to change an accessor. In that case, a large amount of code has to be rewritten if you extensively use the corresponding data member instead of the access function.

Inline Functions

Rec. 24.Access functions should be inline.

Rec. 25.Forwarding functions should be inline.

Rec. 26.Constructors and destructors should not be inline.

The normal reason for declaring a function inlineis to improve its performance. Small functions, such as access functions, that return the value of a class member and so-called forwarding functions that invoke another function should normally be inline.

Correct usage of inline functions may also lead to reduced size of code.

Warning:functions that invoke other inline functions often become too complex for the compiler to be able to make them inline despite their apparent smallness.

This problem is especially common with constructors and destructors. A constructor always invokes the constructors of its base classes and member data before executing its own code. Always avoid inline constructors and destructors unless they are members of a simple base class.

Friends

Rec. 27.Friends of a class should be used to provide additional functions that are best kept outside of the class.

Operations on an object are sometimes provided by a collection of classes and functions.

A friendis a nonmember of a class, which has access to the nonpublic members of the class. Friends offer an orderly way of getting around data encapsulation for a class. A friend class can be advantageously used to provide functions that require data not normally needed by the class.

Suppose there is a list class that needs a pointer to an internal list element in order to iterate through the class. This pointer is not needed for other operations on the list. There may then be reason, such as obtaining smaller list objects, for a list object not to store a pointer to the current list element and instead to create an iterator, containing such a pointer, when it is needed.

One problem with this solution is that the iterator class normally does not have access to the data structures used to represent the list (if they are declared as protectedorprivate).

By declaring the iterator class as friend, this problem is avoided without violating data encapsulation.

Friends are good if used properly. However, the use of many friends can indicate that the modularity of the system is poor.