www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overriding private interface methods

reply "Oleg" <code.viator gmail.com> writes:
Hello. How to override private methods?

import std.stdio, std.conv;

interface abcInterface
{
     private double private_func();
     public final double func() { return private_func(); }
}

class abcImpl: abcInterface
{

}

void main()
{
     auto abc = new abcImpl;
     writeln( abc.func() );
}

This code generate error

non-virtual function

This private method (private_func) is 'private' because it must 
call only in final interface methods (func)
Oct 23 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/23/2012 11:37 AM, Oleg wrote:
 Hello. How to override private methods?

 import std.stdio, std.conv;

 interface abcInterface
 {
 private double private_func();
 public final double func() { return private_func(); }
 }

 class abcImpl: abcInterface
 {

 }

 void main()
 {
 auto abc = new abcImpl;
 writeln( abc.func() );
 }

 This code generate error

 non-virtual function

 This private method (private_func) is 'private' because it must call
 only in final interface methods (func)
private member functions are not virtual by the design of the language. You have to make them 'protected', not private. Ali
Oct 23 2012
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, October 23, 2012 11:47:09 Ali Çehreli wrote:
 private member functions are not virtual by the design of the language.
 You have to make them 'protected', not private.
Yes, but according to TDPL, it's different for interfaces. It specifically talks about using private with interfaces for NVI. And I don't believe that you can just swap it with protected in the case of interfaces (though I could be wrong - I don't remember for sure). Regardless, it doesn't currently work to use private like this for interfaces in spite of the fact that TDPL says that you can, and I'm not quite sure what's going to happen with that in the future. It's clear that private functions in classes will never be virtual, but I'm not sure that the situation is as clear with interfaces. The relevant bug report: http://d.puremagic.com/issues/show_bug.cgi?id=4542 - Jonathan M Davis
Oct 23 2012