When does final mean "maybe, kinda, sorta" ?

Sean Kelly sean at f4.ca
Mon Jan 22 23:58:26 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> Sean Kelly wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>> kris wrote:
>>> [snip]
>>>> In short, it appears the example exhibits some poor practice 
>>>> (overriding private methods), is somewhat misleading (one certainly 
>>>> *hopes* private cannot be seen beyond module-scope), and discusses 
>>>> how to make "final" mean something quite other than final.
>>>
>>> I agree about the misleading part, but I don't think that overriding 
>>> private methods is poor practice. In fact I think it's rich practice 
>>> :o). See e.g. http://www.gotw.ca/publications/mill18.htm.
>>
>> It's a good design approach, but there's no reason the virtual methods 
>> must be private--they could be protected as well.  One could argue 
>> that the design is cleaner with the virtual methods private, but since 
>> the methods must be overridden by the derived class it's not like any 
>> protection attributes are being maintained anyway.  Isn't this a 
>> current topic of discussion on comp.l.c++.m? :-)
> 
> There is a big difference between private and protected. Private means 
> that later-added code cannot call the method, period. It could be said 
> that private is "distantly more private" than both protected and public 
> because both of the latter allow access by unbounded amounts of code.
> 
> So there is indeed something interesting by being required to implement 
> something that you're not allowed to call. Something like Shirley Temple 
> acting in movies she was too young to be allowed to watch :o).

But you can call it, or even change its visibility:

class Base
{
private:
     virtual void fn() = 0;
};

class Derived : Base
{
public:
      virtual void fn() {}
     void call() { fn(); }
};

Sure, you can't call it through Base::fn(), but... :-)

I'll admit it's a clever design pattern, and one I use myself in C++, 
but I think there is also value in privates being implicitly final as in 
D.  No expressive power is really lost--the methods could be made 
protected just as easily--and I think it actually makes more conceptual 
sense that private functions cannot be overridden.


Sean



More information about the Digitalmars-d mailing list