Google C++ Style Guide学习笔记——命名

本文详细介绍了C++编程中的命名规则、文件命名、类型名、变量名、const变量名、函数名、枚举命名等核心内容,旨在提供一份全面的C++编程指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、一般性规则

函数名、变量名、文件名应当是描述性的,避免使用缩写词。

2、文件名

文件名应当全部小写,可以使用下划线'_'或者破折号'-',优先使用下划线。

C++文件应当以.cc结尾

3、类型名

类型名(包括classes、structs、typedefs、enums)以大写字母开头,每个单词首字母大写,不要使用下划线。

4、变量名

变量名全部使用小写,单词与单词之间使用下划线。类的成员变量使用trailing underscores。举例:  

  Common Variable names

   For example:

 string table_name;  // OK - uses underscore.
 string tablename;   // OK - all lowercase.
 string tableName;   // Bad - mixed case.

Class Data Members

Data members (also called instance variables or member variables) are lowercase with optional underscores like regular variable names, but always end with a trailing underscore.

string table_name_;  // OK - underscore at end.
string tablename_;   // OK.

Struct Variables

Data members in structs should be named like regular variables without the trailing underscores that data members in classes have.

struct UrlTableProperties {
  string name;
  int num_entries;
}

See Structs vs. Classes for a discussion of when to use a struct versus a class.

Global Variables

There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with g_ or some other marker to easily distinguish it from local variables.


5、const 变量名

以字母'k'开头,后接首字母大写的大小写混合单词。如:kDaysInAWeek.

6、函数名

    常规函数名:常规函数名应该首字母大写,每个单词以大写字母开头,不使用下划线。如

  	AddTableEntry()
  	DeleteUrl()
  	OpenFileOrDie()
 

Accessors and Mutators

Accessors and mutators (get and set functions) should match the name of the variable they are getting and setting. This shows an excerpt of a class whose instance variable is num_entries_.

class MyClass {
 public:
  ...
  int num_entries() const { return num_entries_; }
  void set_num_entries(int num_entries) { num_entries_ = num_entries; }

 private:
  int num_entries_;
};

You may also use lowercase letters for other very short inlined functions. For example if a function were so cheap you would not cache the value if you were calling it in a loop, then lowercase naming would be acceptable.

7、枚举命名:

像consts或macros一样命名。尽量像consts。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值