private is non-virtual: Stuck in C++-thinking?

foobar foo at bar.com
Fri Oct 19 15:18:28 PDT 2012


On Friday, 19 October 2012 at 21:09:05 UTC, Nick Sabalausky wrote:
> My understanding is that this is intentionally disallowed:
>
> ---------------------------
> module foo;
>
> class Foo
> {
>     private void func() {}
> }
>
> class Bar : Foo
> {
>     // Disallowed:
>     private override void func() {}
> }
>
> void foobar(Foo f)
> {
>     f.func();
> }
> ---------------------------
>
> If D had C++'s "private", that restriction would make a lot of 
> sense
> (except possibly for nested classes, but I dunno). That's 
> because: How
> can you override a class you can't even access?
>
> But D doesn't have a "true" private in the C++ sense. Instead, 
> there
> is code outside a class which *is* permitted to access "private"
> members.
>
> So am I missing something, or was the sample case above 
> overlooked when
> making the "private must be non-virtual" decision?

virtual private is an obscure C++ idiom which I think the 
argument for is extremely week. I think Walter made the right 
decision here in favor of more readable code.

I'd do the following:
---------------------------
module foo;
class Foo {
     private void func() { funcImpl(); }
     protected void funcImpl() {}
}

class Bar : Foo {
     protected override void funcImpl() {}
}

void foobar(Foo f) {
     f.func();
}
---------------------------


More information about the Digitalmars-d mailing list