我如何覆盖< 和> 在 C++/CLI 中?

发布于 2024-07-24 15:46:07 字数 1297 浏览 7 评论 0 原文

我正在移植一个实现 IEquatableIComparable 并覆盖 ==!=<> 从 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);

源文件:

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
}

我不确定为什么原始测试接口中的相等性并比较运算符中的内容,但我试图保留原始结构。

无论如何,我在标记行处收到编译错误:“错误 C2679:二进制 '>' : 找不到采用“ThingNamespace::Thing”类型的右侧操作数的运算符(或没有可接受的转换)”,以及下面两行相应的错误。 为什么它没有发现重载运算符的存在?

I'm porting a class which implements IEquatable<T> and IComparable<T> and overrides ==, !=, < and > from C# into C++/CLI. So far I have:

Header:

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);

Source file:

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
}

I'm not sure why the original tested equality in the interface and compared things in the operator, but I'm trying to preserve the original structure.

Anyway, I get a compilation error at the marked line: "error C2679: binary '>' : no operator found which takes a right-hand operand of type 'ThingNamespace::Thing' (or there is no acceptable conversion)", and a corresponding error two lines below. Why isn't it picking up the existence of the overloaded operator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可是我不能没有你 2024-07-31 15:46:07

this 是一个指针,您需要取消引用它。

if ((*this) > other)
    return 1;
else if ((*this) < other)
    return -1;
else
    return 0;

this is a pointer, you'll need to dereference it.

if ((*this) > other)
    return 1;
else if ((*this) < other)
    return -1;
else
    return 0;
花伊自在美 2024-07-31 15:46:07
return (*this) == other ? 0 : ((*this) > other ? 1 : -1);
return (*this) == other ? 0 : ((*this) > other ? 1 : -1);
一曲爱恨情仇 2024-07-31 15:46:07

正如阿鲁尔所说,您需要取消引用 this 关键字,但顺便说一句,您可能应该在函数参数中使用 const 引用而不是传递对象,因为:

-C++ 按值传递所有对象,而不是按引用传递(这就是发生在 C# 中),因此使用引用可以减少开销。

-它允许您使用标准库中的函数,例如 std::sort,而无需显式指定新的比较运算符

as arul said, you need to dereference the this keyword, but on a side note, you should probably use const references in your function paramaters instead of passing the object since:

-C++ passes all objects by value, not by reference(which is what happens in C#), so using references reduces the overhead.

-It'll let you use functions from the standard library such as std::sort without needing to explicitly specify a new comparison operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文