Compiler doesn't see the inherited opApply in this example

Steven Schveighoffer schveiguy at yahoo.com
Tue Jul 7 11:15:08 PDT 2009


On Fri, 03 Jul 2009 17:42:45 -0400, Mike L. <sgtmuffles at myrealbox.com>  
wrote:

> module test;
>
> import std.stdio;
>
> abstract class Parent
> {
> 	int opApply(int delegate(ref int) dg)
> 	{
> 		int fakeDelegate(ref uint fake, ref int content)
> 			{ return dg(content); }
> 		return opApply(&fakeDelegate);
> 	}
> 	
> 	abstract int opApply(int delegate(ref uint, ref int) dg);
> }
>
> class Child : Parent
> {

/* add this line (see  
http://www.digitalmars.com/d/2.0/function.html#function-inheritance) */
alias Parent.opApply opApply;

> 	override int opApply(int delegate(ref uint, ref int) dg)
> 	{
> 		uint index = 0;
> 		for(int content = 1; content < 6; content++)
> 		{
> 			int result = dg(index, content);
> 			if(result != 0)
> 				return result;
> 			index++;
> 		}
> 		return 0;
> 	}
> }
>
> void main()
> {
> 	version(works)
> 		Parent child = new Child();
> 	else
> 		Child child = new Child();
> 	foreach(int content; child)
> 		writefln(content);
> }


More information about the Digitalmars-d-learn mailing list