第1节:基本数据类型与运算
第2节:函数
第3节:引用与动态内存管理
第4节:C/C++的简化内存模型
第5节:常量
第1节:基本数据类型与运算
Review: typedef and #define
In C99
//C99: no "bool"
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef Blah Blah Blah NewTypeName
3. Names representing types must be in mixed case starting with upper case.
3. 代表类型的名字必须首字母大写并且其它字母大小写混合
例如:Line, SavingsAccount
#define MACRONAME Blah-Blah-Blah
5. Named constants (including enumeration values) must be all uppercase using underscore to separate words.
5. 命名常量(包括枚举值)必须全部大写并由下划线分隔单词
例如:MAX_ITERATIONS, COLOR_RED, PI
Data type : bool ( 布尔类型)
In C++: bool, true, false
bool isMyBook;
bool isRunning = false;
bool isBoy( );
bool hasLicense();
bool canWork();
bool shouldSort();
26. The prefix is should be used for boolean variables and methods.
26. 布尔变量/函数的命名应使用前缀“is”
例如:isSet, isVisible, isFinished, isFound, isOpen
Example of bool (bool 示例)
39. The incompleteness of split lines must be made obvious.
39. 断行必须很明显。
在逗号或运算符后换行,新行要对齐
#include <iostream> int main() { bool isAlpha; isAlpha = false; if (! isAlpha) { std::cout << "isAlpha=" << isAlpha << std::endl; std::cout << std::boolalpha << "isAlpha=" << isAlpha << std::endl; } system("pause"); return 0; }
Type conversion ( 类型转换)
45. Type conversions must always be done explicitly. Never rely on implicit type conversion.
45. 类型转换必须显式声明。永远不要依赖隐式类型转换
例如:floatValue = static_cast<float>(intValue); // NOT: floatValue = intValue;
Declaring and Initializing Variables ( 变量初始化)
int x = 1; //C & C++
int x(1); //C++, OO style
int x; x(1); //Error!
第2节:函数
Default Arguments v.s. Function Declaration
#include <iostream> void printArea(double radius = 1); int main() { printArea(); printArea(4); return 0; } void printArea(double radius = 2) { // Do something }
Default Arguments v.s. Overloading
#include <iostream> using namespace std; int add (int x, int y=10) { return x + y; } int add (int x) { return x + 100; } int main() { add(1); //How to solve this? cin.get(); return 0; }
error C2668: “add”: 对重载函数的调用不明确
inline Functions ( 内联函数)
Using functions in a program:
Advantages(优点): 易读易维护
Drawbacks (缺点): 运行时性能开销
函数调用时:参数及部分CPU寄存器的 寄存器内容进栈,控制流跳转
函数返回时:返回值及寄存器值出栈, 控制流跳转
Inline functions
目的:减小性能开销
方法:代码插入到调用处
结果:导致程序变大
Desire for short functions (适用于短 函数)
NOT suitable for long functions that are called in multiple places ( 不适于多处调用的长函数)
第3节:引用与动态内存管理
51. C++ pointers and references should have their reference symbol next to the type rather than to the name.
51. C++ 指针与引用符号应靠近其类型而非名字。
例如: float* x; // NOT: float *x;
int& y; // NOT: int &y;
Dynamic memory: Allocate/Release
C++中通过运算符new申请动态内存
new < 类型名> (初值) ; // 申请一个变量的空间
new < 类型名>[常量表达式] ; //申请数组
如果申请成功,返回指定类型内存的地址;
如果申请失败,返回空指针(整数0)。
动态内存使用完毕后,要用delete运算符来释放。
delete < 指针名>; //删除一个变量/对象
delete []< 指针名>; //删除数组空间
70. "0" should be used instead of "NULL".
70. 用“0”代替“NULL” is part of the standard C library, but is made obsolete in C++. 。
因为“NULL”是C语言标准库的内容,但是在C++中 已经废止了。
第4节:C/C++的简化内存模型
Simplified Memory Model (C++ 的内存模型)
1. Stack ( 栈)
编译器自动分配释放
2. Heap ( 堆)
一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收
3. Global/Static ( 全局区/静态区)
全局变量和静态变量的存储是放在一块的。
可以简单认为:
程序启动全局/静态变量就在此处
程序结束释放
4. Constant ( 常量区)
可以简单理解为所有常量都放在一起
该区域内容不可修改
第5节:常量
Named Constants ( 命名常量§2.5)
5. Named constants (including enumeration values) must be all uppercase using underscore to separate words.
5. 符号常量(包括枚举值)必须全部大写并用下划线分隔单词
例如:MAX_ITERATIONS, COLOR_RED, PI
Summary ( 总结)
const int * x
int * const y
在前先读,在前不变
* (指针)和 const(常量) 谁在前先读谁 ;
* 代表被指的数据,名字代表指针地址
const在谁前面谁就不允许改变。