www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Accessing a function within an object's superclass from the outside

reply David Zhang <straivers98 gmail.com> writes:
Hello,

Say I have a class, and it overrides a function from its 
superclass. However, the superclass has two overloaded versions 
of the same function. My intent is to override only one of the 
functions, but the second is rendered inaccessible from outside 
of the class. How can I make the overloaded function visible?

ie:

class ClassA {
     void fun(uint a) {}
     void fun(uint a, float b) {}
}

class ClassB: ClassA {
     void fun(uint a) {}
}

ca.fun(a);       //ok
ca.fun(a, b);    //ok
cb.fun(a);       //ok
cb.fun(a, b);    //function fun not callable with uint and float

I seem to remember something about using aliases to fix this, but 
I can't find anything about it either way.
Jan 14
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang wrote:
 Hello,

 Say I have a class, and it overrides a function from its 
 superclass. However, the superclass has two overloaded versions 
 of the same function. My intent is to override only one of the 
 functions, but the second is rendered inaccessible from outside 
 of the class. How can I make the overloaded function visible?

 ie:

 class ClassA {
     void fun(uint a) {}
     void fun(uint a, float b) {}
 }

 class ClassB: ClassA {
     void fun(uint a) {}
 }

 ca.fun(a);       //ok
 ca.fun(a, b);    //ok
 cb.fun(a);       //ok
 cb.fun(a, b);    //function fun not callable with uint and float

 I seem to remember something about using aliases to fix this, 
 but I can't find anything about it either way.
class ClassB: ClassA { alias fun = super.fun; override void fun(uint a) {} }
Jan 14
parent reply David Zhang <straivers98 gmail.com> writes:
On Saturday, 14 January 2017 at 22:17:23 UTC, ketmar wrote:
 class ClassB: ClassA {
   alias fun = super.fun;
   override void fun(uint a) {}
 }
I tried that, but it seems to think I mean to override super.fun(uint) instead of super.fun(uint, float). Looking at my code again, one of them is templated with a range interface. I think that might be the problem, though I can't figure out how to fix it. Wrapping the alias in a template block doesn't seem to do it.
Jan 14
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 14 January 2017 at 22:38:15 UTC, David  Zhang wrote:
 On Saturday, 14 January 2017 at 22:17:23 UTC, ketmar wrote:
 class ClassB: ClassA {
   alias fun = super.fun;
   override void fun(uint a) {}
 }
I tried that, but it seems to think I mean to override super.fun(uint) instead of super.fun(uint, float).
ahem? `void fun(uint a)` == `void fun(uint a)` `void fun(uint a)` != `void fun(uint, float)` of course, you mean to override `void fun(uint a)` with this code.
Jan 14
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
Templates are not virtual. Depending the interface, a different function 
is called:

import std.stdio;

class ClassA {
     void fun(T)(T a) { writeln("ClassA"); }
}

class ClassB: ClassA {
     void fun(uint a) { writeln("ClassB"); }
}

void main() {
     auto cb = new ClassB();
     ClassA ca = cb;

     uint a = 42;
     ca.fun(a);    // calls ClassA.fun
     cb.fun(a);    // calls ClassB.fun
}

Ali
Jan 14
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang wrote:
 I seem to remember something about using aliases to fix this, 
 but I can't find anything about it either way.
So you can alias the names together to merge the overload sets, or at the call site, you can also specify which class's version you want with a dot: cb.ClassA.fun(a, b); // compiles, specifically calls the ClassA method
Jan 14
parent reply Meta <jared771 gmail.com> writes:
On Saturday, 14 January 2017 at 23:31:53 UTC, Adam D. Ruppe wrote:
 On Saturday, 14 January 2017 at 21:55:27 UTC, David  Zhang 
 wrote:
 I seem to remember something about using aliases to fix this, 
 but I can't find anything about it either way.
So you can alias the names together to merge the overload sets, or at the call site, you can also specify which class's version you want with a dot: cb.ClassA.fun(a, b); // compiles, specifically calls the ClassA method
Is this documented anywhere? I had no idea this was a feature.
Jan 14
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 15 January 2017 at 02:32:29 UTC, Meta wrote:
 Is this documented anywhere? I had no idea this was a feature.
Used in some examples here: http://dlang.org/spec/class.html
Jan 15