moc 发表于 2018-9-10 23:02:03

040-C++之类型转换

1、C风格的类型转换
        c风格的类型转换有两种:
隐式类型转换:C语言的自动类型转换,如char --> int
显式类型转换:即强制类型转换,其形式:
        Type b = (TYPE)a;
C-like类型转换,简单且强大。
2、C++风格的类型转换
C++编译器提供了四种类型转换操作来应对不同的场合。
形式:    类型转换关键字<转换后的类型>(需要转的变量);
① static_cast
        静态类型转换,在编译时C++编译器会做类型检查(默认), 基本类型能转换,但是不能转换指针。
②reinterpreter_cast
        重新解释类型,即C中的强制类型转换。
③dynamic_cast
        动态类型转换,安全的基类和子类之间的转换,运行时检查类型。
④ const_cast    常转换, 去除类型的只读属性
3、static_cast 和 reinterpreter_cast
总结: c语言中能隐式类型转换的,在C++中可以用static_cast进行类型转换;C语言中不能隐式类型转换的,在C++中可以用 reinterpreter_cast转换。
C++中的static_cast 和 reinterpret_cast 相当于C中的隐式类型转换和强制类型转换。
int main()
{
        double dpi = 3.141592653;
        int num1 = (int)dpi;                         // c风格类型转换
        int num2 = static_cast<int>(dpi);   // C++静态类型转换C++编译器会自动判断是否可以转换
        int num3 = dpi;                              //C语言中的隐式类型转换,这种类型转换都可以用C++的static_cast
        cout << num1 << endl << num2 << endl;

        // char*==>int*
        char *p1 = (char*)"Hellostatic_cast";
        int *p2 = NULL;

        //p1 = p2;此时c中不能隐式类型转换,需要强制类型转换
        p2 = reinterpret_cast<int*>(p1);            // c++中强制类型转换用reinterpret_cast<>()
        cout << "p1:" << p1 << endl;   //%s
        cout << "p2:" << p2 << endl;   //%d

        system("pause");
        return 0;
}
4、dynamic_cast
动态类型转换运行时类型识别==》转换失败返回NULL
class Tree {};//树类

class Animal
{
public:
        virtual void cry() = 0;
};

class Dog:public Animal
{
public:
        virtual void cry()
        {
                cout << "汪汪" << endl;
        }
        void doHome()
        {
                cout << "看家" << endl;
        }
};

class Cat :public Animal
{
public:
        virtual void cry()
        {
                cout << "喵喵" << endl;
        }
        void doThing()
        {
                cout << "抓老鼠" << endl;
        }
};

void playObj(Animal *pBase)
{
        pBase->cry();
        // 动态类型转换
        Dog* pDog = dynamic_cast<Dog*>(pBase);// 也被称为向下转换
        if (pDog != NULL)
        {
                pDog->doHome();
        }

        Cat* pCat = dynamic_cast<Cat*>(pBase);   
        if (pCat != NULL)
        {
                pCat->doThing();
        }
}

int main()
{
        Dog d1;
        Cat c1;
        playObj(&d1);
        playObj(&c1);
       
        // 复习static_cast 和   reinterpret_cast
        Tree t1;
        Animal *a1;
        //a1 = static_cast<Animal*>(&t1);       // C++编译器做类型检查
        a1 = reinterpret_cast<Animal*>(&t1);//reinterpret_cast 类型重新定义强制类型转换

        system("pause");
        return 0;
}
5、const_cast
去除变量的只读属性。注意:要确保去掉const后的内存空间确实能修改。
void printfBuf(const char * p)
{
        char *p1 = NULL;
        //const char *    ==>char *      // 去掉只读属性
        // 要清楚的知道转换之前是什么类型,转换之后是什么类型
        p1 = const_cast<char*>(p);
        p1 = 'Z';         // 可以通过p1去修改内存空间的值
        cout << p << endl;
}

int main()
{
        char buf[] = "qwertasdf";
        printfBuf(buf);

        // 要确保去掉const后的内存空间确实能修改
        char* str = (char*)"asdfgh";    // 去修改常量区的内存,写入权限报错
        printfBuf(str);

        system("pause");
        return 0;
}
6、总结
1.要清楚的知道要转的变量,转换之前是什么类型,类型转换之后是什么类型,转换之后会产生什么结果;
2.一般情况下,不建议进行类型转换,要避免进行类型转换。
页: [1]
查看完整版本: 040-C++之类型转换