Google C++ Style Guide 读后感

来源:互联网 发布:淘宝系统架构图 编辑:程序博客网 时间:2024/05/21 22:34

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Function_Parameter_Ordering

函数参数定义顺序

When defining a function, parameter order is: inputs, then outputs.

头文件包含顺序

All of a project's header files should be listed as descendants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For example, google-awesome-project/src/base/logging.h should be included as

#include "base/logging.h"

In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:

  1. dir2/foo2.h (preferred location — see details below).
  2. C system files.
  3. C++ system files.
  4. Other libraries' .h files.
  5. Your project's .h files.

With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages.

dir/foo.cc and dir2/foo2.h are often in the same directory (e.g. base/basictypes_test.cc and base/basictypes.h), but can be in different directories too.

Within each section the includes should be ordered alphabetically. Note that older code might not conform to this rule and should be fixed when convenient.

For example, the includes in google-awesome-project/src/foo/internal/fooserver.cc might look like this:

#include "foo/public/fooserver.h"  // Preferred location.#include <sys/types.h>#include <unistd.h>#include <hash_map>#include <vector>#include "base/basictypes.h"#include "base/commandlineflags.h"#include "foo/public/bar.h"
局部变量

Place a function's variables in the narrowest scope possible, and initialize variables in the declaration.

There is one caveat: if the variable is an object, its constructor is invoked every time it enters scope and is created, and its destructor is invoked every time it goes out of scope.

// Inefficient implementation:for (int i = 0; i < 1000000; ++i) {  Foo f;  // My ctor and dtor get called 1000000 times each.  f.DoSomething(i);}

It may be more efficient to declare such a variable used in a loop outside that loop:

Foo f;  // My ctor and dtor get called once each.for (int i = 0; i < 1000000; ++i) {  f.DoSomething(i);}

类的构造函数

Avoid doing complex initialization in constructors (in particular, initialization that can fail or that requires virtual method calls).

Constructors should never call virtual functions or attempt to raise non-fatal failures. If your object requires non-trivial initialization, consider using a factory function or Init() method.

You must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.

We require all single argument constructors to be explicit. Always put explicit in front of one-argument constructors in the class definition: explicit Foo(string name);

The exception is copy constructors, which, in the rare cases when we allow them, should probably not be explicit. Classes that are intended to be transparent wrappers around other classes are also exceptions. Such exceptions should be clearly marked with comments.

拷贝构造函数

If your class does not need a copy constructor or assignment operator, you must explicitly disable them. To do so, add dummy declarations for the copy constructor and assignment operator in the private: section of your class, but do not provide any corresponding definition (so that any attempt to use them results in a link error).

For convenience, a DISALLOW_COPY_AND_ASSIGN macro can be used:

// A macro to disallow the copy constructor and operator= functions// This should be used in the private: declarations for a class#define DISALLOW_COPY_AND_ASSIGN(TypeName) \  TypeName(const TypeName&);               \  void operator=(const TypeName&)

Then, in class Foo:

class Foo { public:  Foo(int f);  ~Foo(); private:  DISALLOW_COPY_AND_ASSIGN(Foo);};

强制转换

Use C++ casts like static_cast<>(). Do not use other cast formats like int y = (int)x; or int y = int(x);.

Use const whenever it makes sense. With C++11, constexpr is a better choice for some uses of const.



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两腮无肉不可交 不交物业费会怎么样 社保交满15年三种情况领不到 手工活不交押金2018免费供料 与朋友交而不信乎 千万不要去西交利物浦 大学该不该上课交手机 不交物业费被起诉后果 房子不住需要交物业费吗 毛坯房交房不合格图片 房产证不满2年交多少税 随身wifi要不要交网费 交不起房租就用妻子东凛 单位交社保 归不归为什么和吴勉绝交 咪表停车不交钱会怎样 金融服务费可以不交吗 2018新规六种情况不用交物业费 贷款买车哪些钱不用交 实名制手机号欠费不交后果 2019新规六种情况不用交物业费 2018新规六种情况不用交暖气费 人不知而不愠不亦君子乎解释 不顾一切居筱亦 人不知而不愠不亦君子乎意思 人不知而不愠不亦君子 人不知而不愠不亦君子乎 神秘爹地惹不起陆亦臣萧玖 神秘爹地惹不起陆亦臣 神秘爹地惹不起萧玖陆亦臣 天不变道亦不变 光年男神不好追江亦寒 萧玖陆亦辰巜神秘爹坨惹不起 不亦君子乎上一句 你若不惜我亦不爱 不亦君子乎 萧玫陆亦臣神秘爹地惹不起 纵浪大化中不喜亦不惧 人不轻狂亦少年辩论赛 简弘亦为什么不红 我已亭亭不忧亦不惧