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:
dir2/foo2.h
(preferred location — see details below).- C system files.
- C++ system files.
- Other libraries'
.h
files. - 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.
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.
- Google C++ Style Guide 读后感
- Google Objective-C Style Guide
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google Objective-C Style Guide 中文版
- Google C++ Style Guide
- Google C++ Style Guide
- Google C++ style guide
- Google c++ style guide
- Google C++ Style Guide
- Google C++ Style Guide
- Google C++ Style Guide
- Google C++ Style Guide
- Google C++ Style Guide
- Android Toast 用法总结
- 获取下拉框的值
- 也谈OpenvSwitch配置GRE隧道
- MyEclipse优化
- 嵌入式学习笔记(19)——AVR单片机之C51向ICCAVR的移植
- Google C++ Style Guide 读后感
- PLSQL Developer V9/V8注册码
- 【示例代码】CSS3实现多种颜色的底部阴影按钮特效
- Android Development Tutorial
- 2011年十个值得收藏的Flash博客
- 汇编常用句式
- AE接口使用理解Geodatabase篇
- VS2010彻底卸载
- XCode Debug