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

user207421
  • 305,947
  • 44
  • 307
  • 483
TYeung
  • 2,579
  • 2
  • 15
  • 30

1 Answers1

5

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.

273K
  • 29,503
  • 10
  • 41
  • 64