I ended up coding (With some help) something like this yesterday:
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo(){cout << "A::foo\n";}
};
class B : private A
{
private:
virtual void foo(){ cout << "B::foo\n";}
void DoSomething(SomeOtherClass& o){o.DoSomething(*static_cast<A*>(this));}
};
I tried changing the inheritance method:
class B : public A
{
private:
virtual void foo(){ cout << "B::foo\n";}
};
int main()
{
A* a = new B;
a->foo();
}
This still works. I expected a compile time error. Please tell me why this is possible and what are the possible uses? I know one use due to first scenario - You can expose different interfaces for different classes.
EDIT:
In the second case, output is B::foo
.