Why private methods cant be virtual?

claptrap clap at trap.com
Tue Sep 22 13:04:35 UTC 2020


On Tuesday, 22 September 2020 at 10:23:08 UTC, Daniel Kozak wrote:
> On Tue, Sep 22, 2020 at 11:06 AM claptrap via 
> Digitalmars-d-learn < digitalmars-d-learn at puremagic.com> wrote:
>
>>
>> "Functions marked as final may not be overridden in a derived 
>> class, unless they are also private"
>>
>> So final private functions can be overriden? It seems not, but 
>> the sentence is definitely confusing if not just plain wrong.
>>
>> Yes they can, if you have class A in one module and class B in 
>> another
> module this will work:
>
> //a.d
> class A
> {
> private final void overrideFun()
> {
> import std.stdio : writeln;
> writeln("A::overrideFun");
> }
> }
>
> //b.d
> import a;
> class B : A
> {
> void overrideFun()
> {
> import std.stdio : writeln;
> writeln("B::overrideFun");
> }
> }
>
> // main.d
> import b;
>
> void main(string[] args)
> {
> B b = new B;
> b.overrideFun;
> }

The thread title is...

"Why private methods cant be virtual?"

IE Not...

"how do I override private functions in a non-polymorphic manner."

And what you suggest wont work because I was asking about virtual 
functions, so I specifically want polymorphism. And FWIW it's no 
big deal I can just use protected, i wasn't looking for a 
solution, I was looking for an explanation as to why it was done 
that way. But apparently there is none.





More information about the Digitalmars-d-learn mailing list