爱站网seo综合查询工具,wordpress-5.2.1,重生做皇帝小说网站,怎么把一个网站的关键词强制类型转换运算符
带着三个疑问阅读#xff1a;
出现的背景是什么#xff1f;何时使用#xff1f;如何使用#xff1f; MSDN . 强制转换运算符 C中的四种强制类型转换符详解 static_cast
(1) 使用场景
在基本数据类型之间转换#xff0c;如把 int 转换为 char#…强制类型转换运算符
带着三个疑问阅读
出现的背景是什么何时使用如何使用 MSDN . 强制转换运算符 C中的四种强制类型转换符详解 static_cast
(1) 使用场景
在基本数据类型之间转换如把 int 转换为 char这种带来安全性问题由程序员来保证在有类型指针与 void * 之间转换用于类层次结构中基类和派生类之间指针或引用的转换。 上行转换派生类----基类是安全的下行转换基类----派生类由于没有动态类型检查所以是不安全的。
(2) 使用特点
主要执行非多态的转换操作用于代替C中通常的转换操作。隐式转换都建议使用 static_cast 进行标明和替换。不能使用 static_cast 在有类型指针内转换。
dynamic_cast
(1) 使用场景
用于将一个父类的指针/引用转化为子类的指针/引用下行转换
(2) 使用特点
基类必须要有虚函数因为 dynamic_cast 是运行时类型检查需要运行时类型信息而这个信息是存储在类的虚函数表中。对于下行转换dynamic_cast 是安全的当类型不一致时转换过来的是空指针而 static_cast 是不安全的。对指针进行 dynamic_cast失败返回 NULL成功返回正常 cast 后的对象指针对引用进行 dynamic_cast失败抛出一个异常成功返回正常 cast 后的对象引用。
const_cast
(1) 使用场景
常量指针或引用与非常量指针或引用之间的转换。
(2) 使用特点
cosnt_cast 是四种类型转换符中唯一可以对常量进行操作的转换符。去除常量性是一个危险的动作尽量避免使用。
reinterpret_cast
(1) 使用场景
用在任意指针或引用类型之间的转换。能够将整型转换为指针也可以把指针转换为整型或数组。
(2) 使用特点
reinterpret_cast 是从底层对数据进行重新解释依赖具体的平台可移植性差。不到万不得已不用使用这个转换符高危操作
示例程序
#include iostreamnamespace staticCast{
/*** brief static_cast的使用场景* 1. 使用static_cast在基本数据类型之间转换* 2. 使用static_cast在有类型指针与void *之间转换* 3. 用于类层次结构中基类和派生类之间指针或引用的转换*/class CBase // 基类(父类)
{};class CDerived : public CBase // 派生类(子类)
{};int useof_static_cast()
{// 1. 使用static_cast在基本数据类型之间转换float fval 10.12;int ival static_castint(fval); // float -- intstd::cout ival std::endl; // out: 10// 2. 使用static_cast在有类型指针与void *之间转换int *intp ival;void *voidp static_castvoid *(intp); // int* -- void*// cout *voidp endl; // error,voidp的大小未知long *longp static_castlong *(voidp);std::cout *longp std::endl; // out: 10// 3. 用于类层次结构中基类和派生类之间指针或引用的转换// 上行转换派生类----基类是安全的CDerived *tCDerived1 nullptr;CBase *tCBase1 static_castCBase*(tCDerived1);// 下行转换基类---- 派生类由于没有动态类型检查所以是不安全的CBase *tCBase2 nullptr;CDerived *tCDerived2 static_castCDerived*(tCBase2); //不会报错但是不安全// 4. 不能使用static_cast在有类型指针内转换float *floatp fval; //10.12的addr//int *intp1 static_castint *(floatp); // error,不能使用static_cast在有类型指针内转换std::cout *floatp std::endl; // out: 10.12
}
}// namespace staticCastnamespace dynamicCast{/*** brief dynamic_cast使用* 1. 用于将一个父类的指针/引用转化为子类的指针/引用下行转换* 2. 当某个成员函数只在子类中存在父类指针指向子类对象。此时该指针想要使用子类的成员函数需要将父类指针转换为子类指针*/
class CBase // 基类(父类)
{
public:// dynamic_cast在将父类cast到子类时父类必须要有虚函数virtual int test() { return 0; } // 一定要是 virtual
};class CDerived : public CBase // 派生类(子类)
{
public:int test() { return 1; }
};int useof_dynamic_cast()
{CBase *p_CBase new CBase; // 基类对象指针CDerived *p_CDerived dynamic_castCDerived *(p_CBase); // 将基类对象指针类型转换为派生类对象指针CBase i_CBase; // 创建基类对象CBase r_CBase i_CBase; // 基类对象的引用CDerived r_CDerived dynamic_castCDerived (r_CBase); // 将基类对象的引用转换派生类对象的引用
}
} //namespace dynamicCastnamespace constCast
{
/*** brief const_cast的使用* **/
int useof_const_cast()
{int value 100;const int *cpi value; // 定义一个常量指针//*cpi 200; // 不能通过常量指针修改值// 1. 将常量指针转换为非常量指针,然后可以修改常量指针指向变量的值int *pi const_castint *(cpi);*pi 200;// 2. 将非常量指针转换为常量指针const int *cpi2 const_castconst int *(pi); // *cpi2 300; //已经是常量指针const int value1 500;const int c_value1 value1; // 定义一个常量引用// 3. 将常量引用转换为非常量引用int r_value1 const_castint (c_value1);// 4. 将非常量引用转换为常量引用const int c_value2 const_castconst int (r_value1);
}
} // namespace constCastnamespace reinterpretCast
{
int useof_reinterpret_cast()
{int value 100;// 1. 用在任意指针或引用类型之间的转换double *pd reinterpret_castdouble *(value);std::cout *pd *pd std::endl;// 2. reinterpret_cast能够将指针值转化为整形值int *pv value;int pvaddr reinterpret_castint(pv);std::cout pvaddr pvaddr std::endl;std::cout pv pv std::endl;
}} // namespaceCastint main(int argc, char* argv[])
{staticCast::useof_static_cast();dynamicCast::useof_dynamic_cast();constCast::useof_const_cast();reinterpretCast::useof_reinterpret_cast();
}