Re:C++ is a hack
by lfowles on Tuesday February 16, 2016 @12:43PM
(#11111632)
I actually tested this one since I had never encountered it in the wild. You can indeed call and get the expected results from the derived classes (now private) member function.
[ Parent
| on HN ]
Just to be clear, that's not in conflict with what the author claimed. You can't call that method using a pointer to the derived class. That would be an access violation, because it was declared private. You need to use a pointer of the superclass type.
struct I_A {
virtual void foo() {}
};
struct A : I_A {
private:
virtual void foo() {}
};
int main() {
A a;
// a.foo(); // compile error
I_A& ia = a;
ia.foo(); // ok
}
To be honest, I find this behaviour useful so it doesn't upset me. It's a decent way to mark off functions that you needed to implement to satisfy an internal interface as not-for-users.Question: Is struct inheritance public by default?