当前位置: 首页 > news >正文

网站建设原因邯郸网站建设的地方

网站建设原因,邯郸网站建设的地方,北京seo平台,网站底部源码类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast const_castreinterpret_castdynamic_caststatic_cast const_cast 被const修饰的函数可以被访问#xff0c;但是不能被修改成员变量 const_cast可以去掉const #include iostream using namespace s… 类型转换const_cast、reinterpret_cast、dynamic_cast、static_cast const_castreinterpret_castdynamic_caststatic_cast const_cast 被const修饰的函数可以被访问但是不能被修改成员变量 const_cast可以去掉const #include iostream using namespace std;class FHello { public:FHello();void Init();/*const*/ private:int a;int b;float c; }; FHello::FHello() {a 0;b 10;c 20.f; } void FHello::Init()/*const*/ {auto Hello_a [](){cout a endl;};auto Hello_b []()-bool{cout b endl;return true;};Hello_a();if (bool bHello Hello_b()){cout true endl;}else{cout false endl;} } int main() {const FHello* HelloTest new FHello();//HelloTest-Init();因为HelloTest被const修饰了而上面代码中没有用const修饰//用const_cast去掉constFHello* Test const_castFHello*(HelloTest);Test-Init();//编译出错会返回NULL//c风格的强转是万能强转//容易出问题万一转换失败了变成野指针了也不知道少用FHello* NewTest (FHello*)HelloTest;NewTest-Init();delete HelloTest;return 0; } reinterpret_cast 把指针转换成int也可以把int转成指针也可以指针转换成指针 #include iostream using namespace std;class FHello { public:FHello();void Init();/*const*/ private:int a;int b;float c; }; FHello::FHello() {a 0;b 10;c 20.f; } void FHello::Init()/*const*/ {auto Hello_a [](){cout a endl;};auto Hello_b []()-bool{cout b endl;return true;};Hello_a();if (bool bHello Hello_b()){cout true endl;}else{cout false endl;} } int main() {//const_castconst FHello* HelloTest new FHello();FHello* Test const_castFHello*(HelloTest);Test-Init();FHello* NewTest (FHello*)HelloTest;NewTest-Init();//reinterpret_cast//reinterpret_cast,先转换成int再转换成指针int a reinterpret_castint(NewTest);FHello*b reinterpret_castFHello*(a);b-Init();//也可以转换成int*指针也可以转换回来int* a2 reinterpret_castint*(NewTest);FHello* b2 reinterpret_castFHello*(a2);b2-Init();//可以转换成float*指针但是不可以用float类型float* a3 reinterpret_castfloat*(NewTest);FHello* b3 reinterpret_castFHello*(a3);b3-Init();delete HelloTest;return 0; }dynamic_cast 动态的转化可以向上转化也可以向下转化 向上转化和向下转化的意思是在数层的继承关系中可以向父类或派生类进行转化 向下转换时会进行类型检测如果类型无法转换回返回NULL。是一个比较安全的转换 接口父类必须有虚函数否则会报错 #include iostream using namespace std;class FHello { public:FHello();virtual ~FHello();void Init();/*const*/ private:int a;int b;float c; }; FHello::FHello() {a 0;b 10;c 20.f; } FHello::~FHello() {} void FHello::Init()/*const*/ {auto Hello_a [](){cout a endl;};auto Hello_b []()-bool{cout b endl;return true;};Hello_a();if (bool bHello Hello_b()){cout true endl;}else{cout false endl;} }class FHello1:public FHello { public:void Hello1temp() {} private:int a; };class FHello2 :public FHello1 {};int main() {//const_castconst FHello* HelloTest new FHello();FHello* Test const_castFHello*(HelloTest);Test-Init();FHello* NewTest (FHello*)HelloTest;NewTest-Init();//reinterpret_castint a reinterpret_castint(NewTest);FHello*b reinterpret_castFHello*(a);b-Init();int* a2 reinterpret_castint*(NewTest);FHello* b2 reinterpret_castFHello*(a2);b2-Init();float* a3 reinterpret_castfloat*(NewTest);FHello* b3 reinterpret_castFHello*(a3);b3-Init();//dynamic_castFHello* h1 new FHello1();//这一步也是向上转换//向下转换FHello1* A dynamic_castFHello1*(h1);A-Hello1temp();//向上转换,用的少因为有更简单的方法如上FHello* B dynamic_castFHello*(A);B-Init();FHello* B2 A;//进行隐式转换FHello2* FHA2 dynamic_castFHello2*(h1);//转换失败输出NULLif (FHA2){}delete HelloTest;delete h1;return 0; }static_cast 可以向上转化也可以向下转化 向下转换时不安全 static_cast不可以转换const、volitale、_unaligned static_cast和dynamic_cast相比static_cast可以没有虚函数而dynamic_cast必须有虚函数 格式 FHello* Astatic_castFHello*(B);
http://www.zqtcl.cn/news/1495/

相关文章:

  • 江苏两学一做网站wordpress慢的原因
  • 网站建设swot分析优秀手机网站版式
  • 网页好看的网站设计建设厅公积金中心网站
  • 金融公司网站源码企业营销理念
  • 怎样做企业官方网站百度网盘可以做网站吗?
  • 龙岩网站建设加盟网站建设柒首先金手指7
  • 广州市品牌网站建设公司做会计公司网站的目录
  • 济南企业做网站wordpress会员付费插件
  • 苏州网站设计聚尚网络门户网站主要包括哪些模块
  • 网站安全评估怎么做学编程入门
  • 做兼职什么网站好品牌策划 网站源码
  • 动物自己做的网站wordpress 无刷新跳转
  • 网站建设的规划手机音乐网站源码
  • 糕点网站策划书魅影视频
  • 汕头网站建设模块五路居网站建设
  • wordpress 全站加密好的摄影网站
  • 网站建设服务流程广西住房及城乡建设厅网站
  • 网站seo知识中小型企业网络搭建案例
  • 花溪建设村镇银行官方网站新开元电销系统
  • 自己做的网站提示危险深圳做自适应网站设计
  • 北海建设厅网站vue做的网站影响收录么
  • 泰安集团网站建设公司营销网站建设实训总结
  • 网站运营专员平面设计专业学什么
  • 简阳网站建设简阳良品铺子vi设计手册
  • 台州网站制作教程如何为网站做面包屑导航
  • 北京微信网站开发报价企业 备案 网站服务内容
  • 天津市建设银行网站wordpress索引插件
  • 哪个网站可以做名片wordpress 登陆插件下载
  • 网站开发的主要流程vi设计公司
  • 西安云众网站建设网站链接 动态图怎么做