Problem with delegates
Zarathustra
adam.chrapkowski at gmail.com
Sun Sep 7 03:54:23 PDT 2008
Acces violation when I try use delegate function.
module test;
private{
// tango.io
import tango.io.Stdout;
}
// class Drawable << super class >>
//______________________________________________________________
private abstract class
Drawable{
// fields
protected const real delegate() m_Cmp;
// overloaded operators
public int
opCmp(Object o){
assert(m_Cmp, "Drawable.m_Cmp can not be null!");
return cast(int)(this.m_Cmp() - (cast(typeof(this))o).m_Cmp());
}
// ctors
public
this(real delegate() o_Cmp){
m_Cmp = o_Cmp;
}
}
// class Point << child class >>
//______________________________________________________________
public final class
Point : Drawable{
// fields
private double x;
private double y;
private double z;
// features
public void
Writefln(){
Stdout.format("x: {}, y: {}, z: {}", x, y, z).newline();
}
// ctors
this(uint o_x, uint o_y, uint o_z){
super( delegate{ return cast(real)this.z; } );
this.x = o_x;
this.y = o_y;
this.z = o_z;
}
}
public void
main(){
try{
Point [] l_point_arr;
l_point_arr ~= new Point(0, 1, 2);
l_point_arr ~= new Point(0, 1, 1);
l_point_arr ~= new Point(0, 1, 4);
l_point_arr ~= new Point(0, 1, 5);
foreach(l_point; l_point_arr){
l_point.Writefln();
}
Stdout.newline();
l_point_arr.sort; // use opCmp property
foreach(l_point; l_point_arr){
l_point.Writefln();
}
}
catch(Object o){
Stdout.format("Fatal Error: {}", o.toString).newline();
}
}
#output#
x: 0.00, y: 1.00, z: 2.00
x: 0.00, y: 1.00, z: 1.00
x: 0.00, y: 1.00, z: 4.00
x: 0.00, y: 1.00, z: 5.00
Fatal Error: Access Violation
More information about the Digitalmars-d-learn
mailing list