现在的位置: 首页 > 综合 > 正文

c++重载 友元等问题

2013年06月07日 ⁄ 综合 ⁄ 共 385字 ⁄ 字号 评论关闭
int n;
lng temp;
struct p
{
    int x;
   friend bool operator < (const p p1, const p p2)
    {
        return p1.x>p2.x;
    }
    bool operator > (const p &p1)
    {
        return x<p1.x;
    }
};

//priority_queue<p> qs;
int main()
{
    p ps[2];
    ps[0].x=22;
    ps[1].x=232;
//    qs.push(ps[0]);
//    qs.push(ps[1]);
//    cout<<qs.top().x<<endl;
  printf("%d\n",ps[1]<ps[0]);
  printf("%d\n",ps[1]>ps[0]);
    return 0;
}

如果在类外需要用到比较的话 不可以用非友元的如下形式,可以把这个函数放在外面也能达到这样的目的

bool operator < (const p p1, const p p2)

抱歉!评论已关闭.