Chapter3: The C in C++
1、利用vector模拟二维数组:
vector<vector<double>> v(m,vector<double>(n)) 是模拟一个含m个数组类型元素的数组,其中每个元素都是一个拥有n个double元素的数组
2、C++中可以随时定义变量;但C中应该在域内最开始定义
3、C++中常量定义:const int x=10,变量在定义时必须被初始化,编译阶段有type checking;C中常量定义:#define PI 31.4,编译阶段没有type checking,只是单纯的替换
4、assert(__condition__) 是一个宏,而不是函数
5、C++中的explicit cast:
static_cast<Typename T> (运用情况:castless conversion,narrowing conversion,conversion from void*)
const_cast<Typename T> (运用情况:conversion from const or volatile)
reinterpret_cast<Typename T> (运用情况:cast to a completely different meaning——most dangerous)
dynamic_cast<Typename T> (运用情况:for type-safe downcasting)
Chapter4: Data Abstraction
1、C与C++中类或结构体的区别:
C结构体:只是将数据成员分装在一起,对外部是完全可见的;用户必须自己定义初始化和清除函数;相关函数必须把要访问的结构体的指针传给函数;易造成结构体之间函数名冲突
C++类:将数据成员与成员函数封装在一起,对外部是部分可见的;通过构造函数和析构函数完成类的构建与清除;成员函数(非static)中有隐含的this指针,可直接访问数据成员,无需显式传递类对象指针;类之间函数名不会发生冲突
2、类对象的size不能为0:
class B{
void f(){ }
};
...
int main(){
B b;
double d; //&b and &d would be the same if the size of B is zero
}
3、C++头文件中structure,class重复声明是不被允许的 (使用preprocessor来解决这一问题)
#ifndef SIMPLE_H
#define SIMPLE_H
struct simple{
...
};
#endif
4、内嵌类或结构体:将相关元素封装在一起
class Stack{
struct Link{
...
}*head;
...
};
5、Global scope resolution:在成员函数中引用一个全局变量
int a;
void f(){ }
struct B{
int a;
void f(){
::f();
::a++;
a--;
}
};
Chapter5: Hiding the Implementation
1、Access control:C++