一.C++类型转换
1.static_cast<目标类型>(转换数据) 用于一般数据类型转换。
2.常量不能修改,const int num=10; 可以修改不生效,直接从常量表中读取,通过const_cast<>()去掉常量属性。
3.指针进行数据类型转换,类型决定了数据解析方式。地址与指针间转换。reinterpret_cast<char *>(转换数据的引用地址)。
4.类的指针的转换,dynamic_cast<>()类的指针之间的转换。
二.inline内联函数
inline内联函数的作用:1.提升效率相当于define 2.类型会严格检查 3.实现模板通用 4.在函数内部展开 5.明确了内联函数无法取地址。
内联函数的限制:1.不能有递归 2.不能有静态数据 3.不包含循环 4.不包含switch和goto语句 5.不包含数据
不满足以上条件则编译器把它当做普通函数。
#include<stdlib.h>
#include<iostream>
#define GETX3(N) N*N*N
inline int getx3(int x);//内联函数内部展开
inline int getx3(int x)//类型安全
{
return x*x*x;
}
template<class T> //通用模板
inline T getx2(T x)
{
return x*x;
}
void main()
{
std::cout << GETX3((1 + 2)) << std::endl;
}