static_cast

static_cast:明确隐式转换
dynamic_cast:父子类转换,运行时类型识别RTTI,有额外的开销,一般在向下转换时才使用,必须要有虚函数
reinterpret_cast:显示强转,后果自负

int main()
{
    int n = 5;
    float f = 10.0f;
    
    f = n;//隐式转换
    
    f = static_cast<float>(n);
//static_cast    
    //低风险的转换
    //整形与浮点型
    //字符与整形
    //void*指针的转换
    //转换运算符的方式
    
    
    int kk;
    char* p;
    p = kk;
    
//reinterpret_cast    
    //高风险的转换
    //整形与指针类型转换
    //不同类型的指针
    //不同类型的引用
    int n;
    int*p = (int*)n;
    int*p = reinterpret_cast<int*>(n);
    //
    
    return 0;
}

 

posted @ 2020-10-18 15:42  zzyoucan  阅读(543)  评论(0编辑  收藏  举报