Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading