Why is the dynamic_cast allowed to yield a null-pointer for polymorphic classes when the destination pointer is not of the type of a base class?

陌路散爱 提交于 2020-01-24 09:00:11

问题


Consider the following program

#include <iostream>
#include <iomanip>

struct A
{
};

struct C
{
};

int main()
{
    C *pc = nullptr;

    A *pa1 = dynamic_cast<A *>( pc );

    std::cout << "pa1 == nullptr is " << std::boolalpha << ( pa1 == nullptr ) << '\n';

    A *pa2 = pc;

    std::cout << "pa2 == nullptr is " << std::boolalpha << ( pa2 == nullptr ) << '\n';
}

For the both pointer declarations, pa1 and pa2, the compiler reports an error that such an initialization is not allowed.

For example the clang HEAD 10.0.0 compiler issues the following errors.

prog.cc:19:14: error: 'C' is not polymorphic
    A *pa1 = dynamic_cast<A *>( pc );
             ^                  ~~
prog.cc:23:8: error: cannot initialize a variable of type 'A *' with an lvalue of type 'C *'
    A *pa2 = pc;
       ^     ~~
2 errors generated.

Now let's make the class C a polymorphic class.

#include <iostream>
#include <iomanip>

struct A
{
};

struct C
{
    virtual ~C() = default; 
};


int main()
{
    C *pc = nullptr;

    A *pa1 = dynamic_cast<A *>( pc );

    std::cout << "pa1 == nullptr is " << std::boolalpha << ( pa1 == nullptr ) << '\n';

    A *pa2 = pc;

    std::cout << "pa2 == nullptr is " << std::boolalpha << ( pa2 == nullptr ) << '\n';
}

And only the second declaration produces an error. The dynamic_cast works.

rog.cc:22:8: error: cannot initialize a variable of type 'A *' with an lvalue of type 'C *'
    A *pa2 = pc;
       ^     ~~
1 error generated.

What is the reason of that such a conversion of pointers for the dynamic_cast is allowed?


回答1:


What is the reason of that such a conversion of pointers for the dynamic_cast is allowed?

Because dynamic_cast runs at run time, and it's too late to issue a compiler error by then.

If the conversion fails, dynamic_cast returns nullptr. You need to check for that and then handle that if needed.



来源:https://stackoverflow.com/questions/59849453/why-is-the-dynamic-cast-allowed-to-yield-a-null-pointer-for-polymorphic-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!