Static casting a pointer to a pointer C++

121 views Asked by At

Would someone help me understand why the second static cast auto cpp = static_cast<Base **>(bpp); fails. The first static cast, nicely checks to ensure that that Der is related to Base. Why does the second static_cast cause the compiler to throw the following error. Thanks!

main.cpp:35:17: error: invalid ‘static_cast’ from type ‘Der**’ to type ‘Base**’

#include <stdio.h>

class Base
{
};

class Der : public Base 
{
};

int main()
{
 
    Base a;
    Der b;
    
    Base * ap = &a;
    Der * bp = &b;
    
    auto cp = static_cast<Base *>(bp);
    
    Base ** app = &ap;
    Der ** bpp = &bp;

    auto cpp = reinterpret_cast<Base **>(bpp);
    //auto cpp = static_cast<Base **>(bpp); // Why does the cause an error?

    printf("Hello World");

    return 0;
}

0

There are 0 answers