语言特性:
1. Templates模板
|
1
2
3
4
5
|
template<class T>inline const T&
max(const T&
a, const T&
b){ return a
< b ? b : a;} |
无类型的模板参数
|
1
2
|
bitset<32>
bit32;bitset<52>
bit51; |
默认模板参数
|
1
2
3
4
|
template<class T, class container=vector<T>
>class MyClass; MyClass<int>
x; //
equal to MyClass<int, vector<int> > |
typename作用
|
1
2
3
4
5
6
7
8
9
10
|
//作为模板形惨template<typename T>class MyClass; //使用嵌套依赖类型,typename后面的是类型名称而不是成员函数或变量template<class T>class MyClass
{ //ptr
is a pointer to type T::SubType typename T::SubType
* ptr;}; |
模板成员member template
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class MyClass
{ template<class T> void f(T);}; template<class T>class MyClass
{private: T
value;public: void assign(const MyClass<T>&
x) { value
= x.value; } //x
必须和*this拥有同样的类型}; void f(){ MyClass<double>
d; MyClass<int>
i; d.assign(d); //ok d.assign(i); //error} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
template<class T>class MyClass
{private: T
value;public: template<class X> void assign(const MyClass<X>&
x) { value
= x.getValue; } T
getValue() const { return value; }}; void f(){ MyClass<double>
d; MyClass<int>
i; d.assign(d); //ok d.assign(i); //ok} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
template<class T>class MyClass<T>
{public: template<class U> MyClass(const MyClass<U>
&other);}; void f(){ MyClass<double>
d; MyClass<double>
d2(d); //call内置的拷贝构造函数 MyClass<int>
i(d); //call
模板构造函数 } |
嵌套模板类
|
1
2
3
4
5
6
|
template<class T>class MyClass
{ ... template<class T2> class NestedClass;}; |
2. 初始化基础类型
|
1
2
3
4
5
6
7
8
9
|
int i1; //没有初始化int i2
= int(); //初始化为0 template<class T>void f(){ //保证了如果T是基础类型,一定初始化为0 T
t = T();}; |
3. 异常处理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Error;void f(){ if(exception) { throw Error(); }} int main(){ try { f(); } catch (const Error&
e) { //处理异常 }} |
4. 命名空间namespace
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//避免名字冲突
引入命名空间namespace FSU
{ class MyClass; void f(); namespace HTTP
{ class HTTPHandle; }} //使用namespace
::int main(){ FSU::MyClass
myClass; FSU::f(); FSU::HTTP::HTTPHandle
handle;} //使用namespace另一种方法//using
namespace FSU;using FSU::MyClass; //推荐使用这种办法 |
5. bool类型
|
1
2
3
|
//只有两个值
true和false//0
is false , and other is truebool flag
= true; |
6. explicit关键字
|
1
2
3
4
5
6
7
8
|
class Stack{ //避免隐式转换 explicit Stack(int size);}; Stack
stack1(50); //okStack
stack2 = 50; //error |
7. C++ 类型转换
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//static_castfloat f;cout
<< static_cast<int>(f)
<< endl; //print
f as int typef(static_cast<string>("string")); //call
f() for string type //dynamic_castclass Car; //Car
至少有一个虚函数,抽象基类class BlueCar
: public Car
{};class RedCar
: public Car
{}; void f(Car*
pCar){ BlueCar*
pBlueCar = dynamic_cast<BlueCar*>(pCar); if(pBlueCar
== NULL) { //pBlueCar
不是指向BlueCar的对象 }} //const_cast//消除const限定const int a
= 9;const int*
pA = &a;int*
b = const_cast<int*>(pA);*b
= 99; //reinterpret_cast
在编译期处理,可能不安全//最常在函数指针类型之间的转换typedef void (*FuncPtr)();FuncPtr
funcPtrArray[10];int doSomething();funcPtrArray[0]
= &doSomething; //error,函数返回值不匹配funcPtrArray[0]
= reinterpret_cast<FuncPtr>(&doSomething);//ok |
8. static const member 的初始化
|
1
2
3
4
5
6
7
|
class MyClass
{public: static const int i
= 9; //only
inteter can}; //定义式const int MyClass::i; //不用再初始化了 |
9. 大O复杂度
| 常量 | O(1) | runtime决定于元素的数目 |
| 对数 | O( log(n) ) | 对数曲线 |
| 线性 | O(n) | 线性增长 |
| n-log-n | O( n*log(n) ) | |
| 平方 | O(n*n) | 平方 |
本文深入探讨了C++中的模板特性和类型转换机制,包括模板参数、初始化基础类型、异常处理、命名空间使用、bool类型、explicit关键字、C++类型转换、静态const成员的初始化以及大O复杂度概念。详细解析了模板类、初始化方式、异常捕获、命名空间避免冲突、bool的二元性、防止隐式转换、类型间转换、const和reinterpret_cast用法,以及常量成员的初始化策略。

被折叠的 条评论
为什么被折叠?



