access subclass functions

Christopher Wright dhasenan at gmail.com
Thu Nov 20 16:35:06 PST 2008


Saaa wrote:
>>
>> from the list (private, protected, public) pick public.
>> Note the difference between peel and peal.
> :)
>>
>> public YellowBanana: Banana
>> {
>>  void doStuff()
>>  {
>>     bool e = peel();   //visible from derived
>>                        //class when defined protected or public.
>>  }
>> }
>>
>> Banana a = new Banana();
>> bool f = a.peel();      //visible only when public
>>
>> All non-static non-private non-template member functions are virtual.
>> (Rule only valid in D)
> What does this mean: to be virtual?

Informally, "virtual" means "can be overridden". It only applies to 
methods on classes. This definition is informal and ambiguous.

According to __traits in d2, "virtual" means "might have a vtbl entry". 
A vtbl is a list of function pointers corresponding to the methods of a 
class. Each instance of that class has a pointer to the same list. 
Virtual dispatch just consists of pointing to a different vtbl with a 
different function in that slot.

Anyway, the __traits definition maps to the following lemmas[1]:
- static is not virtual
- template is not virtual
- private is not virtual

On the other hand, final functions are virtual. If a final function does 
not override a virtual function, the compiler can turn it into a free 
function if that's more efficient. Or maybe not. If a final function 
overrides a virtual function, it will certainly end up in the class vtbl.

Final functions are a bit questionable. If they override a virtual 
method in a base class, they should certainly be considered virtual, but 
you also sometimes need to know whether you can override a function. 
That is when it's not final and it is virtual.


[1] It's a bit of a stretch to grant them this name, but if life gives 
you lemmas, make lemma-ade.


More information about the Digitalmars-d-learn mailing list