x * y not equal to x.operator*(y) ?

Whats the difference between the two lines?

1
2
const MC* mc3 = mc1->operator*(mc2); 	
const MC* mc4 = mc1 * mc2;	


I thought they should be equivalent but my compiler disagrees.

The second line produces the following errors whereas the first line goes throught the compiler without any complaint.

1>c:\dev\test_multipledispatch2\consoleapplication1\source.cpp(83): error C2296: '*' : illegal, left operand has type 'const MC *'
1>c:\dev\test_multipledispatch2\consoleapplication1\source.cpp(83): error C2297: '*' : illegal, right operand has type 'const MC *'
It's because mc1 is a pointer and you have not defined an operator* that takes two MC*. In fact, I don't think you can do that. Normally operators takes references to class objects instead of pointers as argument.

1
2
3
4
5
6
7
struct MC
{
	MC operator(const MC& mc);
};
...
MC mc3 = mc1->operator*(*mc2); 	
MC mc4 = *mc1 * *mc2;
Last edited on
In the second line you are trying to multiply built-in types that are const pointers.
In the frist line you are multiplying in fact the object *mc1 by the pointer mc2
I think that the second line would be equivalent to the first line you should to write

const MC * mc4 = *mc1 * mc2;
Thank you guys!
Topic archived. No new replies allowed.