1 函数模板
namespace tl
{
template <typename T> void swap(T & t1, T & t2)
{
T tmpT;
tmpT = t1;
t1 = t2;
t2 = tmpT;
}
}
2 类模板
template <class T> class Stack {
public:
Stack();
~Stack();
void push(T t);
T pop();
bool isEmpty();
private:
T *m_pT;
int m_maxSize;
int m_size;
};
3 类型转换
关键字 | 说明 |
---|---|
static_cast | 用于良性转换,一般不会导致意外发生,风险很低。 |
const_cast | 用于 const 与非 const、volatile 与非 volatile 之间的转换。 |
reinterpret_cast | 高度危险的转换,这种转换仅仅是对二进制位的重新解释,不会借助已有的转换规则对数据进行调整,但是可以实现最灵活的 C++ 类型转换。 |
dynamic_cast | 借助 RTTI,用于类型安全的向下转型(Downcasting)。 |
4 空指针nullptr
c++11弃用NULL,使用nullptr,避免冲突,NULL可以转化未数值0,nullptr只表示空指针
5 常量表达式constexpr
constexpr int a = 5;
int arr[a]; //合法
6 auto自动类型转换
auto i = 5;
auto ci = new auto(5);
auto cj = new int[5];
7 区间迭代
区间迭代是指基于范围的 for 循环
for(auto & item:arr)
{
cout<<item<<endl;
}
final
则是为了防止类被继续继承以及终止虚函数继续重载引入的