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

Style Classes

Rule 27. All methods in class declarations must be grouped by their meaning using MFC style grouping conventions: constructors/destructors, attributes, operations, overridables, implementation, and in the mentioned order.

Exception:MS Visual C++ AppWizard and ClassWizard insert specially formatted comment delimiters in your source code files to mark the places in your files that ClassWizard can write to. For instance,AFX_VIRTUALis used to mark the beginning and end of virtual function override declarations in a class's include file. Do not take member functions out of those sections.

Rule 28.Member functions must be declared before member data.

Rec. 17.Group member functions and data in sections that contain similar kinds of class members.

Rec. 18.Avoid large function implementations within the class declaration.

By gathering class members in sections, you guide a user through the class declaration. It helps him to find quickly what he wants. See Example 15and“MFC: Using the MFC Source Files”(MSDN Library, Programming with MFC: Encyclopedia) for more information.

A member function that is implemented within a class declaration automatically becomes inline. Class declarations are less compact and more difficult to read when they include many implementations of member functions. It is easier for an inline member function to become an ordinary member function if its implementation is placed outside of the class declaration.


Rule 29.The return type of a function must be explicitly provided.

Rule 30.The names of formal arguments to functions must be specified and must be the same both in the function declaration and in the function implementation.

Rule 31.A function body must not be implemented on the same line as the function name.

Exception:An empty function body may be implemented on the same line as the function name.

Rule 32.Always write the left parenthesis directly after a function name and the right parenthesis directly after the last argument.

Rule 33.Function arguments must be separated by spaces.

Rec.19.Import and export functions of a regular DLL should be declared asextern "C".

Rec. 20.Explicitly specify all functions from the global namespace with the scope resolution prefix::.

Rec. 21.When declaring functions, the leading parenthesis should be written on the same line as the function name. If space permits, other arguments and the closing parenthesis should also be written on the same line as the function name.

See also Rule 34.

If no return type is explicitly provided for a function, it is, by default, an int. It is recommended always providing the return type explicitly, to increase the readability of the code.

The names of formal arguments may be specified in both the function declaration and the function implementation in C++, even if these are ignored by the compiler in the declaration. Providing names for function arguments is a part of the function documentation. The name of an argument may clarify how the argument is used, reducing the need to include additional comments in, for example, a class declaration. It is also easier to refer to an argument in the class documentation if it has a name. See Examples 16.

If a function body is placed on the same line with the function definition, it is impossible to step into such a function during the debugging process. It is the only reason why such implementations are forbidden. See Example 17.

Exception specification is described in section 15.4 of the ANSI C++ draft. It allows specifying all the exceptions that a function may directly or indirectly throw. Documenting those exceptions makes the code more readable and explicitly indicates which exceptions should be caught by a caller. See Example 18.

Warning:At this time the implementation details of exception specification have not been standardized, and are accepted but not implemented in Microsoft Visual C++. Microsoft Visual C++ compiler warning C4290 explicitly states it. You can disable this warning by using the warning pragma:#pragma warning(disable: 4290).

C++ uses the same calling convention and parameter-passing techniques as C, but naming conventions are different because of C++ decoration of external symbols. By causing C++ to drop name decoration, the extern "C"syntax makes it possible for a C++ module to share data and routines with other languages.

By directly specifying that a function belongs to the global namespace, we make code more readable. It is strongly recommended for the Windows API functions to distinguish them from member functions.

The other recommendations are meant to give a uniform appearance to the written code. Until formatting tools are available, programmers should follow these guidelines. See Example 19andMiscellaneous.

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.