changes of operator overloading in vc++2005
in vc++2003, managed c++ operator overloading of value type use the CLS function, such as op_Addition.
but, in vc++2005, it seems that operator overloading of value type is changed back to the traditional way. it uses key word operator, such as operator+.
i tried vc++2003 way in 2005 and i got the error below:
Error 1 error C3195: 'op_Addition' : is reserved and cannot be used as a member of a ref class or value type. CLR operators must be defined using the 'operator' keyword
I don't know if i am right at this point. so, if anyone got any idea, please tell me. thx.
[590 byte] By [
Riekey] at [2007-12-16]
Riekey, in C++/CLI, you use the standard C++ operator overloading:
ref struct R{
R(int x):i(x){} int i;
static R^ operator+(R^ lhs, R^ rhs){
return gcnew R(lhs->i + rhs->i);
}
};
int main(){
R^ a = gcnew R(10);
R^ b = gcnew R(20);
R^ c = a+b;
System::Console::WriteLine(c->i); //30
}
When the operator above is output into metadata, it goes out as op_Addition. But you declare it in C++ using the standard C++ operator overloading.