All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be
#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_
...
#endif // FOO_BAR_BAZ_H_
If a source or header file refers to a symbol defined elsewhere, the file should directly include a header file which properly intends to provide a declaration or definition of that symbol. It should not include header files for any other reason.
不要依赖传递包含,如果文件main.cc 需要bar.h 中符号,那main.cc 就要包含bar.h, 不管是否有包含的foo.h已经包含了bar.h
定义内联函数的条件是函数在十行以下或者更少。
定义内联函数可以产生非常有效的代码,常用的是将存取和设置函数定义为内联函数。
文件名都用小写,可以包含下划线或者短横线。可以接受的文件名如下
my_useful_class.cc
my-useful-class.cc
myusefulclass.cc
myusefulclass_test.cc // _unittest and _regtest are deprecated.
源文件以.cc 结尾,头文件以.h结尾
Do not use filenames that already exist in /usr/include, such as db.h.
In general, make your filenames very specific. For example, use http_server_logs.h rather than logs.h. A very common case is to have a pair of files called, e.g., foo_bar.h and foo_bar.cc, defining a class called FooBar.
Type names start with a capital letter and have a capital letter for each new word, with no underscores: MyExcitingClass, MyExcitingEnum.
The names of all types — classes, structs, type aliases, enums, and type template parameters — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores.
// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...
// typedefs
typedef hash_map<UrlTableProperties *, std::string> PropertiesMap;
// using aliases
using PropertiesMap = hash_map<UrlTableProperties *, std::string>;
// enums
enum class UrlTableError { ...
变量名包括class和struct成员名都是小写,可以包含下划线。
std::string table_name; // OK - lowercase with underscore.
std::string tableName; // Bad - mixed case.
Data members of classes, both static and non-static, are named like ordinary nonmember variables, but with a trailing underscore.
class TableInfo {
...
private:
std::string table_name_; // OK - underscore at end.
static Pool<TableInfo>* pool_; // OK.
};
Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have. 结构数据成员命名与一般变量相同。
struct UrlTableProperties {
std::string name;
int num_entries;
static Pool<UrlTableProperties>* pool;
};
常量以k开头,后边跟驼峰命名。下划线可以用,但很少用到。
const int kDaysInAWeek = 7;
const int kAndroid8_0_0 = 24; // Android 8.0.0
函数名使用驼峰命名法,存取和设置函数使用普通变量命名。
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
c++ 中一般不要使用宏,若要使用的的话,它的命名规则是全部大写,可以使用下划线。
#define ROUND(x) ...
#define PI_ROUNDED 3.0
枚举应该像常量一样命名。不要跟宏一样的命名。
enum class UrlTableError {
kOk = 0,
kOutOfMemory,
kMalformedInput,
};
2009年之前的枚举跟宏命名方式相同,这导致了一些错误。因此枚举使用常量的方法进行。
private:
// Used to bounds-check table accesses. -1 means
// that we don't yet know how many entries the table has.
int num_total_entries_;
// The total number of test cases that we run through in this regression test.
const int kNumTestCases = 6;
因篇幅问题不能全部显示,请点此查看更多更全内容