zoukankan      html  css  js  c++  java
  • c++ ,类型转换

    一.隐式转换

    1)精度低转高,sigend转unsigend
    2)数值0,会转为为指针. 数组名会转换为首地址.
    3)bool转换 。0为false.其他为true.-1也是true...
    4)非const转为const.
    5)标准库定义的转换.
    while(cin>>s)

    二。显示转换。

    1)最常用的static_cast() 可以代替任何隐式转换。

    2)dynamic_cast 运行时识别指针或引用所指的对象

    3)const_cast ,添加或删除const特性。

    4)reinterpret_cast ,操作数的位模式提供较低层次的重新解释。

    #include <stdio.h>
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    
    //二。显示转换。
    //
    //1)最常用的static_cast 可以代替任何隐式转换。
    //3)const_cast ,添加或删除const特性。
    
    //2)dynamic_cast 运行时识别指针或引用所指的对象(记得后面要补充例子)
    //4)reinterpret_cast ,操作数的位模式提供较低层次的重新解释。(记得后面要补充例子)
    
    
    
    int main()
    {
        //static_cast
        char a='a';
        int b=static_cast<int>(a);
        cout<<b<<endl;
    
        //string 的万能转换
        string str="3.5";
        stringstream ss;
        ss<<str;
        double dou;
        ss>>dou;
        cout<<dou<<endl;
    
        //const_cast,把const 的指针转为非const.那么就可以用新指针 修改 数据了.
        const int* cint=new int(3);
        int* cint2=  const_cast<int*>(cint);
        *cint2 =4;
        cout<<*cint<<endl;
    
    
        return 0;
    }
  • 相关阅读:
    linux下的make命令
    安装pytorch
    CondaError: Downloaded bytes did not match Content-Length
    cv2.VideoCapture(0)
    cv2.imread(),cv2.imshow(),cv2.imwrite()
    unsqueeze()和squeeze()
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module
    Can't connect to MySQL server on '127.0.0.1' (10061)
    ping ip
    _vimrc配置
  • 原文地址:https://www.cnblogs.com/lsfv/p/6007190.html
Copyright © 2011-2022 走看看