如何重载 == 运算符以允许在多重比较中使用它?

发布于 2024-09-28 22:00:33 字数 719 浏览 6 评论 0原文

我正在尝试重载 == 运算符来比较如下所示的对象。

class A
{
    int a;
public:
    A(int x) { a = x; }
    bool operator==(const A& obRight)
    {
        if(a == obRight.a)
        {
            return true;
        }
        return false;
    }
};

int main()
{
    A ob(10), ob2(10), ob3(10);
    if(ob == ob2) // This equality comparison compiles fine.
        cout<<"Equal"<<endl;
     if(ob == ob2 == ob3) //This line doesn't compile as overloaded 
                          // == operator doesn't return object (returns bool)
           cout<<"Equal"<<endl;
}

正如我上面所描述的,我无法在一行中进行多个对象比较
就像 if(ob == ob2 == ob3) 通过成员函数使用重载 == 运算符。

我应该使用友元函数重载吗?

I am trying to overload == operator to compare objects like below.

class A
{
    int a;
public:
    A(int x) { a = x; }
    bool operator==(const A& obRight)
    {
        if(a == obRight.a)
        {
            return true;
        }
        return false;
    }
};

int main()
{
    A ob(10), ob2(10), ob3(10);
    if(ob == ob2) // This equality comparison compiles fine.
        cout<<"Equal"<<endl;
     if(ob == ob2 == ob3) //This line doesn't compile as overloaded 
                          // == operator doesn't return object (returns bool)
           cout<<"Equal"<<endl;
}

As i described above, i am unable to do multiple object comparison in a single line
like if(ob == ob2 == ob3) using overloaded == operator through member function.

Should i overload using friend function ?

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

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

发布评论

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

评论(3

自我难过 2024-10-05 22:00:33

不,你从根本上误解了你的操作。

if (ob == ob2 == ob3) =
if (ob == (ob2 == ob3)

想想类型。

if (A == (A == A))
if (A == bool) // Oh dear, no == operator for bool!

你需要有

if ((ob == ob2) && (ob == ob3))
if ((A == A) && (A == A))
if (bool && bool) // fine

No. You fundamentally misunderstood your operation.

if (ob == ob2 == ob3) =
if (ob == (ob2 == ob3)

Think about the types.

if (A == (A == A))
if (A == bool) // Oh dear, no == operator for bool!

You need to have

if ((ob == ob2) && (ob == ob3))
if ((A == A) && (A == A))
if (bool && bool) // fine
诗化ㄋ丶相逢 2024-10-05 22:00:33

通常,您在实际代码中不应该这样做
因为用法与其他人的期望完全不同。意外的事情是不直观的,并且不直观使得代码对于不熟悉代码库的人来说难以维护(或理解)。

但作为一项学术练习。

您想要的是让运算符 == 返回一个对象,这样如果在另一个测试中使用它,它将执行测试,但如果它只是留在布尔上下文中,那么它将自动转换为布尔值。

#include <iostream>
using namespace std;

template<typename T>
class Test
{
    public:
        Test(T const& v, bool s)
            :value(v)
            ,state(s)
    {}

    Test operator==(T const& rhs) const
    {
        return Test<T>(value, state && value == rhs);
    }
    operator bool() const
    {
        return state;
    }
    private:
        T const&    value;
        bool        state;
};

class A
{
    int a;
    public:
    A(int x) { a = x; }
    Test<A> operator==(const A& obRight) const
    {
        return Test<A>(*this, a == obRight.a);
    }
};

int main()
{
    A ob(10), ob2(10), ob3(14);
    if(ob == ob2) // This equality comparison compiles fine.
        cout<<"Equal"<<endl;
    if(ob == ob2 == ob3) 
        cout<<"Equal"<<endl;
}

As a rule you SOULD NOT DO THIS in real code.
As the usage is completely different from what other people are expecting. Unexpected things are non-intuitive, and non-intuitive makes the code hard to maintain (or understand) for somebody that is not familiar with the code base.

But as an academic exercise.

What you want is to get the operator == to return an object so that if it is used in another test it will do the test but if it is just left in a boolean context then it will auto convert to bool.

#include <iostream>
using namespace std;

template<typename T>
class Test
{
    public:
        Test(T const& v, bool s)
            :value(v)
            ,state(s)
    {}

    Test operator==(T const& rhs) const
    {
        return Test<T>(value, state && value == rhs);
    }
    operator bool() const
    {
        return state;
    }
    private:
        T const&    value;
        bool        state;
};

class A
{
    int a;
    public:
    A(int x) { a = x; }
    Test<A> operator==(const A& obRight) const
    {
        return Test<A>(*this, a == obRight.a);
    }
};

int main()
{
    A ob(10), ob2(10), ob3(14);
    if(ob == ob2) // This equality comparison compiles fine.
        cout<<"Equal"<<endl;
    if(ob == ob2 == ob3) 
        cout<<"Equal"<<endl;
}
迟月 2024-10-05 22:00:33

您可以创建一个这样的函数

#include<stdarg.h>
template<class T>
bool equals(size_t num, T val,...)
{
    va_list arguments;

    va_start(arguments,num);

    for(size_t i = 0; i<num; ++i)
        if(val != va_arg(arguments, int))
        {
            va_end ( arguments );
            return false;       
        }
    va_end ( arguments );
    return true;
}

并使用它

if(equals(3,ob1,ob2,ob3))
    //do some stuff here

You can create a function like this

#include<stdarg.h>
template<class T>
bool equals(size_t num, T val,...)
{
    va_list arguments;

    va_start(arguments,num);

    for(size_t i = 0; i<num; ++i)
        if(val != va_arg(arguments, int))
        {
            va_end ( arguments );
            return false;       
        }
    va_end ( arguments );
    return true;
}

and use it

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