Cast Object - get null

Namespace rswhite4 at googlemail.com
Wed Apr 18 09:58:42 PDT 2012


override bool opEquals(Object o) const {
         if (o is null) {
		return false;
	}
	
	writeln(o); // write: cast.Vector2D!(float).Vector2D
	
	Vector2D!(T) vec = cast(Vector2D!(T)) o;
	
	writeln(vec); // write: null
         // ...

It seems the cast fail, but the question is: why?

Here my test context:

void main() {
	alias Vector2D!(short) Vector2s;
	alias Vector2D!(float) Vector2f;
	
	Vector2f vf = Vector2f(23, 42);
	
	Vector2s vs = Vector2s(vf);
	
	writefln("vs.x: %d, vs.y: %d", vs.x, vs.y);
	
	Vector2s vs2 = Vector2s(23, 42);
	
	if (vs2 == vf) {
		writeln("equal");
	}
}

Vector2D is my own class. If i compare vs2 with vs it works fine,
but if i compare vs or vs2 with vf, the cast fail and i get a
null-reference. How can i avoid this?


More information about the Digitalmars-d-learn mailing list