digitalmars.D.learn - Accessing a function within an object's superclass from the outside
- David Zhang (20/20) Jan 14 2017 Hello,
- ketmar (5/25) Jan 14 2017 class ClassB: ClassA {
- David Zhang (7/11) Jan 14 2017 I tried that, but it seems to think I mean to override
- ketmar (5/12) Jan 14 2017 ahem?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (17/17) Jan 14 2017 Templates are not virtual. Depending the interface, a different function...
- Adam D. Ruppe (6/8) Jan 14 2017 So you can alias the names together to merge the overload sets,
- Meta (2/11) Jan 14 2017 Is this documented anywhere? I had no idea this was a feature.
- Adam D. Ruppe (3/4) Jan 15 2017 Used in some examples here:
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 2017
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 2017
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 2017
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: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.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).
Jan 14 2017
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 2017
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 2017
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:Is this documented anywhere? I had no idea this was a feature.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 2017
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 2017