第1节:不可变对象、不可变类;避免多次声明
第2节:实例成员与静态成员
第3节:析构函数与友元
第4节:拷贝构造函数
第5节:示例分析
第6节:vector 类
第7节:更多编码规范
第1节:不可变对象、不可变类;避免多次声明
4. Variable names must be in mixed case starting with lower case.
4. 变量名必须混合大小写且以小写字母开头
例如:line, savingsAccount
How to make a class immutable? ( 让类成为“不可变类”)
Mark all data fields private (所有数据域 均设置为“私有”属性)
全是我的!
No mutator functions (没有更改器函数)
谁都不准动!
No accessor that would return a reference/pointer to a mutable data field object ( 也没有能够返回可变数据域对象的 引用 或指针的访问器)
偷摸碰也不 行!
用于多线程编程immutable object: thread-safe
Preventing Multiple Declarations ( 避免多次声明)
1. Put "#pragma once" in the first line of .h file ( 使用“杂注”)
依赖于编译器
古老的编译器不支持
2. Use #ifndef preprocessing instructions in .h file ( 使用“宏”)
#ifndef FILENAME_H
#define FILENAME_H
// The contents of the header file
#endif FILENAME_H
第2节:实例成员与静态成员
Rules for Static member function ( 使用静态成员函数的规则)
Rules1: How to invoke Static member function: (调用静态成员函数)
Rules2: Static member functions access other members: (静态成员函数访问 其他成员)
#include <iostream> class A { public: A(int a = 0)//构造函数 { x = a; } static void f1();//静态成员函数 static void f2(A a);//静态成员函数 private: int x; static int y;//静态数据成员 }; void A::f1() { std::cout << A::y << std::endl; } void A::f2(A a) { std::cout << A::y << std::endl; std::cout << a.x << std::endl; } int A::y = 25;//初始化静态数据成员 void main() { A::f1(); A mA(3); A::f2(mA); mA.A::f1(); system("pause"); }
Instance or Static? ( 实例还是静态)
When to use STATIC in class? (何时在类中使用静态成员)
A variable or function that is not dependent on a specific instance of the class should be a static variable or function. ( 变量和函数不依赖于类的实例时)
For example
every circle has its own radius. Radius is dependent on a specific circle. Therefore, radius is an instance variable of the Circle class. Since the getArea function is dependent on a specific circle, it is an instance function.
Since numberOfObjects is not dependent on any specific instance, it should be declared static.
第3节:析构函数与友元
Destructors ( 析构函数)
Destructors are the opposite of constructors. (dtor vs ctor)
第4节:拷贝构造函数
Copy Constructors
class X { //C++03/11: 12.8 // ... public: X(int); X(const X&, int = 1); }; X a(1); // calls X(int); X b(a, 0); // calls X(const X&, int); X c = b; // calls X(const X&, int);
Shallow Copy vs. Deep Copy ( 浅拷贝和深拷贝)
Shallow copy: if the field is a pointer to some object, the address of the pointer is copied rather than its contents. ( 拷指针,而非指针指向的内容)
Deep copy: Copy the contents that pointed by the pointer (拷指针指向的内容)
Customizing Copy Constructor( 定制拷贝构造函数)
Shallow copy:
default copy constructor (默认构造函 数)
assignment operator for copying = (用 于拷贝对象的赋值运算符)
Deep copy: you should implement the copy ctor. ( 自行编写拷贝构造 函数)
第5节:示例分析
第6节:vector 类
The C++ vector Class
Limitation of using array to store values: the array size is fixed in the class declaration. ( 用数组存放数据时,容量大小不可变)
The vector object can increase its size if needed.(vector对象容量可自动增大)
Examples
std::vector<int> intVector; // Store numbers 1, ..., 10 to the vector for (int i = 1; i < 10; i++) { intVector.push_back(i + 1); }
25. Iterator variables should be called i, j, k etc.
25. 迭代变量名应该用 i, j, k 等
此外,变量名 j, k应只被用于嵌套循环
尽量多用i做循环变量;
尽量令i的作用域限制在循环块内
第7节:更多编码规范
Two basic guidelines ( 两个最基础的规范)
1. Any violation to the guide is allowed if it enhances readability.
1. 只要能增强可读性,你在编码时可以不遵守这些编程风格指南
一语道尽规范的目的
2. The rules can be violated if there are strong personal objections against them.
2. 如果你有很好的个人理由的话,可以不遵守这些规范
Why do I violate the rules? ( 为嘛我要违反规则)
46. Variables should be initialized where they are declared.
46. 变量应在其声明处初始化
71. Basic indentation should be 2.
Indentation of 1 is too small to emphasize the logical layout of the code. Indentation larger than 4 makes deeply nested code difficult to read and increases the chance that the lines must be split. Choosing between indentation of 2, 3 and 4, 2 and 4 are the more common, and 2 chosen to reduce the chance of splitting code lines.
73. The class declarations should have the following form :
class SomeClass : public BaseClass { public: ... protected: ... private: ... }
Layouts for loop statements ( 循环语句的布局)
74. Method definitions should have the following form :
74. 方法定义应遵循如下形式
void someMethod() { ... }
76. A for statement should have the following form : for (initialization; condition; update) { statements; } 77. An empty for statement should have the following form : for (initialization; condition; update) ; This emphasizes the fact that the for statement is empty and it makes it obvious for the reader that this is intentional.Empty loops should be avoided however. 78. A while statement should have the following form : while (condition) { statements; } 79. A do - while statement should have the following form : do { statements; } while (condition);
About white space ( 关于空格)
84.
‐ Conventional operators should be surrounded by a space character. (运算符前后应有空格)
‐ C++ reserved words should be followed by a white space.(C++ 保留字后边应有空格)
‐ Commas should be followed by a white space. (逗号后面跟空格)
‐ Colons should be surrounded by white space.(冒号前后有空格)
‐ Semicolons in for statments should be followed by a space character.(for 语句的分号后有空格)
例如:
a = (b + c) * d; // NOT: a=(b+c)*d while (true) // NOT: while(true) { ... doSomething(a, b, c, d); // NOT: doSomething(a,b,c,d); } case 100: // NOT: case 100: for (i = 0; i < 10; i++) { // NOT: for(i=0;i<10;i++){ ... }