太一的博客

一个程序学徒

从C到C++(四)

内联函数

  • 当程序执行函数调用时,系统要建立栈空间,保护现场,传递参数以及控制程序执行的转移等等,这些工作需要系统时间和空间的开销。有些情况下,函数本身功能简单,代码很短,但使用频率却很高,程序频繁调用该函数所花费的时间却很多,从而使得程序执行效率降低。
  • 为了提高效率,一个解决办法就是不使用函数,直接将函数的代码嵌入到程序中。但这个办法也有缺点,一是相同代码重复书写,二是程序可读性往往没有使用函数的好。
  • 为了协调好效率和可读性之间的矛盾,C++ 提供了另一种方法,即定义内联函数,方法是在定义函数时用修饰词 inline
1
2
3
4
5
6
inline int max(int a, int b)
{
return a > b ? a : b;
}

#define MAX(a, b) (a) > (b) ? (a) : (b)

内联函数与带参数宏区别

  • 内联函数调用时,要求实参和形参的类型一致,另外内联函数会先对实参表达式进行求值,然后传递给形参;而宏调用时只用实参简单地替换形参
  • 内联函数是在编译的时候、在调用的地方将代码展开的,而宏则是在预处理时进行替换的
  • C++ 中建议采用 inline 函数来替换带参数的宏

新的类型转换运算符

  • 旧式转型
    • (T)expr
    • T(expr)
  • 新式转型
    • const_cast<T>(expr)
    • static_cast<T>(expr)
    • reinterpret_cast<T>(expr)
    • dynamic_cast<T>(expr)
      • 执行“安全向下”转型操作,也就是说支持运行时识别指针或所指向的对象,这是唯一一个无法用旧式语法来进行的转型操作

const_cast

  • 用来移除对象的常量性(cast away the constness
  • const_cast 一般用于指针或者引用
  • 使用 const_cast 去除 const 限定的目的不是为了修改它的内容
  • 使用 const_cast 去除 const 限定,通常是为了函数能够接受这个实际参数
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
33
34
#include <iostream>

using namespace std;

void fun(int &val)
{
cout << "fun" << endl;
}

int main(void)
{
const int val = 100;
// int n = const_cast<int>(val); // error: invalid conversion from 'const int*' to 'int*'
int n = val; // ok

// int* p = &val; // error: invalid conversion from 'const int*' to 'int*'

int* p = const_cast<int*>(&val);
*p = 200; // 只是修改了临时空间

cout << &val << endl;
cout << p << endl;
cout << val << endl; // 100

const int val2 = 200;
// int& refval2 = val2; // error
int& refval2 = const_cast<int&>(val2);
refval2 = 300;
cout << val2 << endl; // 200
// fun(val2); // error
fun(refval2);

return 0;
}

static_cast

  • 编译器隐式执行的任何类型转换都可以由 static_cast 完成
  • 当一个较大的算数类型赋值给较小的类型时,可以用 static_cast 进行强制转换
  • 可以将 void* 指针转换为某一类型的指针
  • 可以将基类指针指向派生类指针
  • 无法将 const 转化为 nonconst,这个只有 const_cast 才可以办到
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main(void)
{
int n = static_cast<int>(3.14);
cout << "n = " << n << endl;

void* p = &n;

int* p2 = static_cast<int*>(p);
return 0;
}