On 10/26/2026 9:21 PM, Dan Cross wrote:
> term% ruby
> class A
> def foo
> bar
> end
>
> private
> def bar
> puts "Hello from A's bar"
> end
> end
>
> class B < A
> private
> def bar
> puts "Hello from B's bar"
> end
> end
>
> a = A.new
> a.foo
> b = B.new
> b.foo
> Hello from A's bar
> Hello from B's bar
> In *this* language, `private` doesn't mean what it means in C++.
In Ruby it means object private instead of class private. And as
demonstrated in my other post then it is a OO encapsulation
disaster.
But the second example quoted here is actually not different from C++.
Because C++ interprets visibility as just that and does not
include overrideability.
#include <iostream>
using namespace std;
class A
{
private:
virtual void bar() { cout << "Hello from A's bar" << endl; }
public:
void foo() { bar(); }
};
class B : public A
{
private:
void bar() { cout << "Hello from B's bar" << endl; }
};
int main()
{
A *a = new A();
a->foo();
B *b = new B();
b->foo();
return 0;
}
also outputs:
Hello from A's bar
Hello from B's bar
It has very little practical impact as those C++
developers that really want to limit bar to A
just omit the virtual.
The construct of private virtual is still used though
because some very smart people have found out that
base classes with public non-virtual methods and
private pure virtual methods has some advantages.
It provides better encapsulation.
Anyway - it does not really relate much to the topic
at hand - just wanted to note that for this particular
example Ruby actually is like C++.
Arne