opMul

Simen Kjaeraas simen.kjaras at gmail.com
Sun Mar 2 16:47:33 PST 2008


On Sun, 02 Mar 2008 20:10:23 +0100, Denton Cockburn <diboss at hotmail.com>  
wrote:

> Is there a reason why opMul cannot be called on a constant object?
>
> This restriction is hardcoded (I clearly don't have a choice for my  
> object).


That would be due to opMul not being defined as a const function.


import std.stdio;

struct Foo1
{
	int bar;
	
	Foo1 opMul(int rhs)
	{
		bar *= rhs;
		return *this;
	}
}

struct Foo2
{
	int bar;
	
	Foo2 opMul(int rhs) const // difference here
	{
		bar *= rhs;
		return *this;
	}
}

void main()
{
	const Foo1 foo1 = Foo1(4);
	Foo1 bar1 = foo1 * 3;	// fails

	const Foo2 foo2 = Foo2(4);
	Foo2 bar2 = foo2 * 3;	// works
}



More information about the Digitalmars-d mailing list