c++ style

来源:互联网 发布:数据采集系统方案 编辑:程序博客网 时间:2024/05/16 08:18

1.When defining a function, parameter order is: inputs, then outputs. 参数的顺序:输入在前,输出在后

2.Use standard order for readability and to avoid hidden dependencies:C library, C++ library, other libraries' .h, your project's .h. 包含文件的顺寻:c库,c++库、其它库、自己的h文件

3.Prefer nonmember functions within a namespace or static member functions to global functions; use completely global functions rarely. 命名空间中定义非成员函数或者静态成员函数,尽量不使用全局函数。

4.Static or global variables of class type are forbidden: they cause hard-to-find bugs due to indeterminate order of construction and destruction.不要使用静态或者全局类:因为其无法确定的构建、销毁顺序。

5.In general, constructors should merely set member variables to their initial values. Any complex initialization should go in an explicit Init() method.初始化不要放在构建函数中,要放在init函数中。

6.Use the C++ keyword explicit for constructors with one argument.Copy Constructors .Provide a copy constructor and assignment operator only when necessary. Otherwise, disable them with DISALLOW_COPY_AND_ASSIGN. 显示声明构造函数;拷贝构造函数和赋值操作符,如果不许要则禁止。

7.Use a struct only for passive objects that carry data; everything else is a class. 搭载data的时候使用struct,其他使用class

8.Composition is often more appropriate than inheritance. When using inheritance, make it public.继承不如包含,继承的时候使用public

9.Make data members private, and provide access to them through accessor functions as needed (for technical reasons, we allow data members of a test fixture class to be protected when using Google Test). Typically a variable would be called foo_ and the accessor function foo(). You may also want a mutator function set_foo(). Exception: static const data members (typically called kFoo) need not be private.

10.Use the specified order of declarations within a class: public:before private:, methods before data members (variables), etc. public在前,private在后,方法在前,数据成员(变量)在后

11.Use 0 for integers, 0.0 for reals, NULL for pointers, and '\0' for chars.

12.Use sizeof(varname) instead of sizeof(type) whenever possible.

原创粉丝点击