zoukankan      html  css  js  c++  java
  • 无代码 无说服力 4种 cast

         #include <iostream>

         using namespace std;

     

         class Base

         {

         public:

             int _base;

             virtual void printinfo()

             {

                  cout << _base << endl;

             }

         };

     

         class Derived : public Base

         {

         public:

             int _derived;

             virtual void printinfo()

             {

                  cout << _derived << endl;

             }

         };

     

         int main(void)

         {

             Base b1;

             Derived d1;

             int aInt = 10;

             long aLong = 11;

             float aFloat = 11.11f;

             double aDouble = 12.12;

     

             Derived* pd = static_cast<Derived*>(&b1);                       

       // down-casting          不安全

             Base* pb = static_cast<Base*>(&d1);                                 

     // up-casting                安全

             Derived& d = static_cast<Derived&>(b1);                            

    // down-casting          不安全

             Base& b = static_cast<Base&>(d1);                                     

    // up-casting                安全

     

             aInt = static_cast<int>(aFloat);                                               

    // 基本数据类型转换

             void* sth = static_cast<void*>(&aDouble);                         

      // double指针类型转换成void指针类型

             double* bDouble = static_cast<double*>(sth);                   

    // void指针类型转换成double指针类型

             cout << *bDouble << endl;c

     

             Base* pb1 = dynamic_cast<Base*>(&d1);

             //Derived* pd1 = dynamic_cast<Derived*>(&b1);                

    // 编译时有warning,运行时出错

     

             int bInt = reinterpret_cast<int>(pb1);                                      

    // 将地址或指针转换成整数

             cout << bInt << endl;

             pb1 = reinterpret_cast<Base*>(bInt);                                    

    // 将整数转换成地址或指针

     

             int* cInt = reinterpret_cast<int*>(&aFloat);                          

      // 这个转换的结果会出乎意料

             cout << (int)*cInt << endl;

     

             const Base* bBase = new Base();

             Base* cBase = const_cast<Base*>(bBase);

             //Base* dBase = dynamic_cast<Base*>(bBase);              

     // 不能通过编译

             //Base* eBase = static_cast<Base*>(bBase);                

        // 不能通过编译

             //Base* fBase = reinterpret_cast<Base*>(bBase);          

      // 不能通过编译

     

             return 0;

    }

  • 相关阅读:
    微服务架构技术栈选型手册(万字长文)
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    Visual Studio 2013 always switches source control plugin to Git and disconnect TFS
    MFC对话框中使用CHtmlEditCtrl
    ATL开发 ActiveX控件的 inf文件模板
    ActiveX: 如何用.inf和.ocx文件生成cab文件
    Xslt 1.0中使用Array
    如何分隔两个base64字符串?
    An attempt was made to load a program with an incorrect format
    JQuery 公网 CDN
  • 原文地址:https://www.cnblogs.com/titer1/p/2407936.html
Copyright © 2011-2022 走看看