C++语法糖
C++
const
- const变量
1
2
3// expression must be a modifiable lvalue
int const *p;//指针内容不可改
int* const p;//指针不可改变指向 - const修饰函数
1
2
3
4// 可以直接echo("hello world");调用
// 如果没有const会报错
// a reference of type string(not const-qualified) can not be initailized with a value of const char *
void echo(const string& str); - const修饰this指针
1
2
3
4
5
6
7
8
9
10
11
12
13class Base
{
public:
void setHeight() const
{
//expression must be a modifiable lvalue
height = 3;
}
private:
int height;
int weight;
};
static
- 作用域
1
2// 全局变量只在当前文件内可见
static int global_val = 0;
cast
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast
new vs malloc
- placement new
object-oriented
- 继承
- 封装
- 多态
reflection
- typeid
- type_info
技巧性
- 设计一个只能在堆上生成的对象
- 设计一个只能在堆上生成的对象
必要性
- virtual destructor
1
2
3
4
5
6class Base
{
public:
Base();
virtual ~Base();//用基类的指针删除派生类对象,会调用派生类析构函数
};
破坏性
- friend
- mutable
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.