自定义类CItem初始化类实例,如何判断两个类实例是否相等

解决方案 »

  1.   

    http://zhidao.baidu.com/question/94488975.html
      

  2.   

    class A
    {
        friend bool operator == (const A& a1, const A& a2);private:
        int _a1;
        int _a2;
    }bool operator == ( const A& a1, const A& a2 )
    {
        if( a1._a1 == a1._a1 && a1._a2 == a2._a2 )
            return true;
        return false;
    }
      

  3.   


    memcmp,直接比较两个实例的数据
      

  4.   

    问题问的比较含糊,类相等或类比较,你得有个标准。是里面的数据一样,还是说完全是一个类实例?如果是比较数据,自己写if语句也可以搞定,或者用操作符重载。如果是比较内存地址,一个==就ok。
      

  5.   


    想要唯一,就在类里面添加一个编号id,判断的时候看看id是不是一样class A
    {
    A(void)
    {
    id = _idx++;
    }
    int id;
    static int _idx;
    }int A::_idx = 0;//初始化,要放到cpp文件里