09.C++类型转换

简介: (创建于2017/12/31)C++类型转换 static_cast 普遍情况 const_cast 去常量 dynamic_cast 子类类型转为父类类型 reinterpret_cast 函数指针转型,不具备移植性1.

(创建于2017/12/31)

C++类型转换

    static_cast 普遍情况
    const_cast 去常量
    dynamic_cast 子类类型转为父类类型
    reinterpret_cast 函数指针转型,不具备移植性

1.static_cast 普遍情况

#include <iostream>

using namespace std;
//原始类型转换,所有情况都是一种写法,可读性不高,有可能有潜在的风险

void* func(int type){   
    switch (type){
    case 1: {
        int i = 9;
        return &i;
    }
    case 2: {
        int a = 'X';
        return &a;
    }
    default:{
        return NULL;
    }

    }   
}

void func2(char* c_p){
    cout << *c_p << endl;
}

void main(){    
    //int i = 0;
    //自动转换
    //double d = i;
    //double d = 9.5;
    //int i = d;

    //int i = 8;
    //double d = 9.5;
    //i = static_cast<int>(d);
    
    //void* -> char*
    //char* c_p = (char*)func(2);
    //char* c_p = static_cast<char*>(func(2));

    //C++ 意图明显
    func2(static_cast<char*>(func(2)));
    //C
    func2((char*)(func(2)));
    
    system("pause");
}
  1. const_cast 去常量
void func(const char c[]){
    //c[1] = 'a';
    //通过指针间接赋值
    //其他人并不知道,这次转型是为了去常量
    //char* c_p = (char*)c;
    //c_p[1] = 'X';
    //提高了可读性
    char* c_p = const_cast<char*>(c);
    c_p[1] = 'Y';

    cout << c << endl;
}

void main(){
    char c[] = "hello";
    func(c);

    system("pause");
}

3.dynamic_cast 子类类型转为父类类型

class Person{
public:
    virtual void print(){
        cout << "人" << endl;
    }
};

class Man : public Person{
public:
    void print(){
        cout << "男人" << endl;
    }

    void chasing(){
        cout << "泡妞" << endl;
    }
};


class Woman : public Person{
public:
    void print(){
        cout << "女人" << endl;
    }

    void carebaby(){
        cout << "生孩子" << endl;
    }
};

void func(Person* obj){ 

    //调用子类的特有的函数,转为实际类型
    //并不知道转型失败
    //Man* m = (Man*)obj;
    //m->print();

    //转型失败,返回NULL
    Man* m = dynamic_cast<Man*>(obj);   
    if (m != NULL){
        m->chasing();
    }

    Woman* w = dynamic_cast<Woman*>(obj);
    if (w != NULL){
        w->carebaby();
    }
}

void main(){
    
    Woman w1;
    Person *p1 = &w1;

    func(p1);

    system("pause");
}

4.reinterpret_cast 函数指针转型,不具备移植性

void func1(){
    cout << "func1" << endl;
}

char* func2(){
    cout << "func2" << endl;
    return "abc";
}

typedef void(*f_p)();

void main(){
    //函数指针数组
    f_p f_array[6];
    //赋值
    f_array[0] = func1;

    //C方式
    //f_array[1] = (f_p)(func2);
    //C++方式
    f_array[1] = reinterpret_cast<f_p>(func2);

    f_array[1]();

    system("pause");
}

相关文章
|
29天前
|
设计模式 安全 算法
【C/C++ 类型转换 】深入理解C++向上转型:从基础到应用
【C/C++ 类型转换 】深入理解C++向上转型:从基础到应用
41 0
|
30天前
|
安全 编译器 程序员
特殊类设计以及C++中的类型转换
特殊类设计以及C++中的类型转换
28 2
|
1月前
|
安全 编译器 C++
【C/C++ 类型转换规则】一文了解C/C++ 中的类型转换规则,帮助你更好的编程
【C/C++ 类型转换规则】一文了解C/C++ 中的类型转换规则,帮助你更好的编程
18 0
|
1天前
|
安全 编译器 C语言
【C++高阶(九)】C++类型转换以及IO流
【C++高阶(九)】C++类型转换以及IO流
|
1月前
|
算法 Linux 编译器
【C++ 泛型编程 进阶篇】 C++ 模版元编程 类型转换 std::decay 全面教程
【C++ 泛型编程 进阶篇】 C++ 模版元编程 类型转换 std::decay 全面教程
42 0
|
1月前
|
安全 编译器 C语言
【C++ 类型转换关键字 *_cast 】理解const_cast、reinterpret_cast、dynamic_cast和static_cast的用法
【C++ 类型转换关键字 *_cast 】理解const_cast、reinterpret_cast、dynamic_cast和static_cast的用法
27 0
|
安全 编译器 程序员
【C++】—— C++的类型转换
【C++】—— C++的类型转换
|
1月前
|
安全 编译器 程序员
C++类型转换
C++类型转换
10 0
|
1月前
|
C++
c++类型转换
c++类型转换
53 1
|
2月前
|
存储 C++
C++ 操作重载与类型转换(二)
C++ 操作重载与类型转换(二)
42 2

热门文章

最新文章