Sim*_*mon 1 c++-cli operator-overloading
我移植它实现了一个类IEquatable<T>和IComparable<T>和重写==,!=,<和>从C#转换成C++/CLI.到目前为止,我有:
标题:
virtual bool Equals(Thing other);
virtual int CompareTo(Thing other);
static bool operator == (Thing tc1, Thing tc2);
static bool operator != (Thing tc1, Thing tc2);
static bool operator > (Thing tc1, Thing tc2);
static bool operator < (Thing tc1, Thing tc2);
Run Code Online (Sandbox Code Playgroud)
源文件:
bool Thing ::Equals(Thing other)
{
// tests equality here
}
int Thing ::CompareTo(Thing other)
{
if (this > other) // Error here
return 1;
else if (this < other)
return -1;
else
return 0;
}
bool Thing ::operator == (Thing t1, Thing t2)
{
return tc1.Equals(tc2);
}
bool Thing ::operator != (Thing t1, Thing t2)
{
return !tc1.Equals(tc2);
}
bool Thing::operator > (Thing t1, Thing t2)
{
// test for greater than
}
bool Thing::operator < (Thing t1, Thing t2)
{
// test for less than
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么原始测试界面中的相等性并比较运算符中的东西,但我试图保留原始结构.
无论如何,我在标记的行中得到一个编译错误:" error C2679: binary '>' : no operator found which takes a right-hand operand of type 'ThingNamespace::Thing' (or there is no acceptable conversion)",以及下面两行的相应错误.为什么它不会存在重载运算符?
this 是一个指针,你需要取消引用它.
if ((*this) > other)
return 1;
else if ((*this) < other)
return -1;
else
return 0;
Run Code Online (Sandbox Code Playgroud)