在C ++中使用reinterpret_cast下调 [英] Downcasting using reinterpret_cast in C++

查看:71
本文介绍了在C ++中使用reinterpret_cast下调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想更详细地了解下降因素,我知道不建议这样做,因为它会导致不确定的运行时行为。我们何时真正需要使用浇铸法?

I'm just trying to understand the down casting in more details, I understand that it is not advisable as it leads to undefined run time behavior. When do we really need to use down casting?

还要考虑代码片段和down_cast的dynamic_cast返回值,但是当我使用reinterpret_cast时,它正确地打印了派生类函数,怎么办?仅当通过值而不是通过引用或指针分配对象时,才进行对象切片吗?

Also consider the coding snippet and the dynamic_cast returns for the down casting but when I use reinterpret_cast it prints out the derived class function rightly, how? The object slicing will take in place only when assigning objects by value but not by reference or pointers?

#include <iostream>
using namespace std;

class base
{
    public:
    void func1()
    {
        std::cout<<"\n Base Function"<<std::endl;
    }
    virtual ~base(){cout<<"\n Base Destructor"<<endl;}
};

class derived:public base
{
    public:
    void func2()
    {
        std::cout<<"\n Derived Function"<<std::endl;
    }
};

int main() 
{

    base *ptr = dynamic_cast<base*>(new derived); // Up casting
    if (ptr)
    {
        ptr->func1();
    }
    else
    {
        cout<<"\n casting failed"<<endl;
    }

    derived *ptr1 = dynamic_cast<derived*>(new base); // Down casting
    if (ptr1)
    {
        ptr1->func2();
    }
    else
    {
        cout<<"\n ptr1 casting failed"<<endl;
    }

    return 0;
}


推荐答案

向下转换本身不会导致不确定的行为。如果您有指向base的指针或引用,则可以使用dynamic_cast并确保仅在它确实是派生的情况下才起作用。如果不是派生类,则将得到nullptr或异常(在投射引用时)。一切都会在运行时检查。

A downcasting by itself does not lead to undefined behavior. If you have a pointer or reference to base, you can use a dynamic_cast and be sure that it will only work if it really is a derived. If it is not a derived you will get a nullptr or an exception (when casting a reference). Everything will be checked at runtime.

如果您已经知道基本指针是派生的,则可以改用static_cast。但是,将不执行任何检查,以免导致基指针的未定义行为不是派生的。

If you already know that your base pointer is a derived, you could use a static_cast instead. However no checks will be performed so that you can cause undefined behavior of your base pointer is not a derived.

reinterpret_cast不应用于向下转换。与static_cast一样,将不执行运行时检查,但也不会执行编译时检查。您可能会意外地跨继承树进行转换(例如,从基类到other_base)。
对于单继承,reinterpret_cast将像static_cast一样工作,但是当使用多继承时,reinterpret_cast不会修改指向对象内部正确基址的指针,并且您具有未定义的行为。

The reinterpret_cast should not be used for downcasting. As with static_cast, no runtime check will be performed, but also no compile-time check is done. You could accidentally cast across inheritance trees (e.g. from base to other_base). For the case of single inheritance, reinterpret_cast will work like a static_cast, but when multiple inheritance is used, reinterpret_cast will not modify the pointer to the correct base inside the object and you have undefined behaviour.

当可以使用static_cast代替时,我建议远离reinterpret_cast。

I would recommend to stay away from reinterpret_cast when you can use static_cast instead.

这篇关于在C ++中使用reinterpret_cast下调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆