当前位置: 代码迷 >> 综合 >> C++ <static_cast、reinterpret_cast、dynamic_cast、const_cast> 类型转换
  详细解决方案

C++ <static_cast、reinterpret_cast、dynamic_cast、const_cast> 类型转换

热度:15   发布时间:2024-02-27 17:51:25.0

类型转换

      • static_cast
      • reinterpret_cast
      • dynamic_cast
      • const_cast
  • 正在更新中。。。

static_cast

#include <iostream>using namespace std;class Animal
{
    
public:virtual void cry() = 0;
};class Cat : public Animal
{
    
public:virtual void cry(){
    cout << "喵喵喵" << endl;}
};class Dog :public Animal
{
    
public:virtual void cry(){
    cout << "汪汪汪" << endl;}
};int main(void)
{
    //父子类型转换Dog* dog1 = new Dog();Animal* a1 = static_cast<Animal*>(dog1);//a1->cry();Dog* dog1_1 = static_cast<Dog*>(a1);Cat* cat1 = static_cast<Cat*>(a1);Dog dog2;Animal& a2 = static_cast<Animal&>(dog2);Dog& dog2_2 = static_cast<Dog&>(a2);//普通类型转换int t = 100;char c = static_cast<char>(t);//空指针类型转换int* ip = static_cast<int*>(NULL);Dog* dp = static_cast<Dog*>(NULL);//指针类型转换int* p = new int[10];void* vp = static_cast<void*>(p);vp = p;system("pause");return 0;
}

reinterpret_cast

#include <iostream>using namespace std;class Animal
{
    
public:void cry(){
    cout << "动物叫" << endl;}
};class Cat : public Animal
{
    
public:void cry(){
    cout << "喵喵喵" << endl;}
};class Dog :public Animal
{
    
public:void cry(){
    cout << "汪汪汪" << endl;}
};int main(void)
{
    int* p = reinterpret_cast<int*>(0x9999);int val = reinterpret_cast<int>(p);Dog dog1;Animal* a1 = &dog1;a1->cry();Dog* dog1_p = reinterpret_cast<Dog*>(a1);Dog* dog2_p = static_cast<Dog*>(a1);Cat* cat1_p = static_cast<Cat*>(a1);//Cat* cat2_p = static_cast<Cat*>(dog1_p);Cat* cat2_p = reinterpret_cast<Cat*>(dog1_p);Animal& a2 = dog1;Dog& dog3 = reinterpret_cast<Dog&>(a2);dog1_p->cry();dog2_p->cry();cat1_p->cry();(*cat2_p).cry();system("pause");return 0;
}

dynamic_cast

#include <iostream>using namespace std;class Animal
{
    
public:virtual void cry(){
    cout << "动物叫" << endl;}
};class Cat : public Animal
{
    
public:void cry(){
    cout << "喵喵喵" << endl;}void play(){
    cout << "爬爬树" << endl;}
};class Dog :public Animal
{
    
public:void cry(){
    cout << "汪汪汪" << endl;}void play(){
    cout << "溜达溜达" << endl;}
};//指针void animalPlay(Animal* animal)
{
    animal->cry();Dog* pDog = dynamic_cast<Dog*>(animal);if (pDog){
    pDog->play();}else{
    cout << "不是狗" << endl;}Cat* pCat = dynamic_cast<Cat*>(animal);if (pCat){
    pCat->play();}else{
    cout << "不是猫" << endl;}
}//引用
void animalPlay(Animal& animal)
{
    animal.cry();try{
    Dog& pDog = dynamic_cast<Dog&>(animal);pDog.play();}catch (const std::bad_cast bc){
    cout << "不是狗,应该是猫" << endl;}try{
    Cat& pCat = dynamic_cast<Cat&>(animal);pCat.play();}catch (const std::exception&){
    cout << "不是猫,应该是狗" << endl;}
}int main(void)
{
    Dog* dog1 = new Dog();Animal* a1 = dog1;Dog dog2;animalPlay(dog2);Cat* cat1 = new Cat();Animal* a2 = cat1;Cat cat2;animalPlay(cat2);system("pause");return 0;
}

const_cast

#include <iostream>using namespace std;void demo(const char* p)
{
    char* p1 = const_cast<char*>(p);p1[0] = 'A';cout << p << endl;
}void demo(const int p)
{
    int q = p;cout << p << endl;
}int main(void)
{
    char p[] = "12345678";demo(p);system("pause");return 0;
}

正在更新中。。。