c++downcast

Why static casting Derived to Base with private inheritance causes a conversion between "const Derived" to "const Base"?


class Base {
};
class Derived: private Base {
};
int main() {
    Derived d;
    static_cast<Base>(d);
}

I understand that such a cast is an error because of private inheritance. However, what I am interested in is why the error message is:

error: cannot cast 'const Derived' to its private base class 'const Base'

In particular, why is it not casting 'Derived' to 'Base'? Why is there a const here? Thanks in advance.


Solution

  • static_cast<Base>(d) calls the implicit copy constructor Base(const Base&) with the argument Derived d, that is passed by a reference const Derived& and can't be further converted to const Base& by the well known reason to you.