www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pure opEquals in a class

reply werter <werter gmail.com> writes:
The code below doesn't work. Is it possible to make a pure 
opEquals in a class?

void main()
{
	class A
	{
		bool a;
		int b;
		
		this(bool g, int h)
		{
			a = g;
			b = h;
		}
				
		pure bool opEquals(const A rhs) const
		{
			return b == rhs.b;	
		}		
	}
		
	A a = new A(true, 5);
	A b = new A(false, 5);
	
	assert(a == b); //fails
}
Aug 20 2018
parent ag0aep6g <anonymous example.com> writes:
On Monday, 20 August 2018 at 19:36:15 UTC, werter wrote:
 The code below doesn't work. Is it possible to make a pure 
 opEquals in a class?
[...]
 		pure bool opEquals(const A rhs) const
 		{
 			return b == rhs.b;
 		}
It doesn't work because `rhs` has the wrong type. It must be `Object`. override pure bool opEquals(const Object rhs) const { const A a = cast(A) rhs; return b == a.b; }
Aug 20 2018