Could you please explain why following code does compile and works fine (checked on gcc-4.3.4). I thought selective inheritance cannot weaken or even strengthen access to members/methods. Doesn’t it break encapsulation rules?
#include <iostream>
class A {
protected:
void foo() { std::cout << "foo" << std::endl; }
};
class B : private A {
public:
using A::foo; //foo() becomes public?!
};
int main() {
B b;
b.foo();
return 0;
}
From the language point of view, there’s nothing wrong with this (whether it’s good design is another matter).
Any class can choose to expose to a wider audience things that it has access to.
In principle, your example is no different to:
except that here
foo()is exposed by proxy.What you can’t do is the opposite: a publicly derived class can’t restrict access to things that are accessible via the base class.