operator

Posted heben

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了operator相关的知识,希望对你有一定的参考价值。

例1,最简单的情况,重载==,以判断两个对象是否相等

#include "stdafx.h"
#include <iostream>
using namespace std;

class person
{
private:
    long id;
public:
    person(long id)
    {
        this->id = id;
    }
    bool operator == (const person& person) const
    {
        if (this->id == person.id)
            return true;
        return false;
    }
};

int main_20180715_0216()
{
    person p1(1);
    person p2(1);
    cout << "p1 == p2 ? " << (p1 == p2) << endl;
    cin.get();
    return 0;
}

 

以上是关于operator的主要内容,如果未能解决你的问题,请参考以下文章