Casting pointer to derived class to reference to pointer to base class

前端 未结 4 1733
星月不相逢
星月不相逢 2021-01-17 02:47

Why I can\'t cast a pointer to derived class to reference to pointer to base class?

struct Base { };
struct Derived : Base { };

int main()
{
    Derived* de         


        
4条回答
  •  暖寄归人
    2021-01-17 03:23

    if you want a reference you need to do

    Base* ptr = static_cast(derived);
    Base &ref = *ptr;
    

    you cant cast a pointer to a reference - they are quite different things

提交回复
热议问题