non virtual interfaces

Ali Çehreli acehreli at yahoo.com
Thu Sep 19 22:45:42 PDT 2013


On 09/19/2013 10:31 PM, Alexandr Druzhinin wrote:

 > if I use protected instead of private in interface like:

private member functions are non-virtual.

 > interface Transmogrifier
 > {
 >      final void thereAndBack()
 >      {
 >          transmogrify();
 >          untransmogrify();
 >      }
 >
 >      protected:
 >          void transmogrify();
 >          void untransmogrify();
 > }

If they were non-virtual (i.e. private), the calls to transmogrify() and 
untransmogrify() from thereAndBack() would be bound to 
Transmogrifier.transmogrify and Transmogrifier.untransmogrify at compile 
time. That happens and the linker cannot find their definitions.

 > class CardboardBox: Transmogrifier
 > {
 >      override protected void transmogrify() { }
 >      override void untransmogrify() {}
 > }

 > it compiles, but why does compiler permit making untransmogrify() be
 > public?

It is up to CardboardBox to decide whether untransmogrify() is public or 
not. Note that untransmogrify() is still protected when objects are used 
through the Transmogrifier interface. However, when an object is known 
to be a CardboardBox so that it is being used through the CardboardBox 
interface, it is not bound to be a Transmogrifier at that point. Yes, 
CardboardBox inherits from Transmogrifier but it is CardboardBox's 
interface that is being used at that point so it decides.

 > How can I prohibit this? May be it just unrealized yet?

You cannot prohibit from Transmogrifier.

Ali



More information about the Digitalmars-d-learn mailing list