如何制作与指针一起使用的重载运算符 [英] How to make an overload operator which works with pointers

查看:71
本文介绍了如何制作与指针一起使用的重载运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主题中,我需要使用指针的运算符,因此我不必调用 *a>*b 而是 a>b.例如我的操作符 << 与指针一起工作:

As in the subject I need operator which will work with pointers so I do not have to call *a>*b but a>b. For example my operator << works with the pointers ok:

friend ostream& operator<< (ostream &wyjscie, Para const* ex){
    wyjscie << "(" << ex->wrt << ", " << ex->liczbaWystapien <<")"<< endl;
    return wyjscie;
}

但是这个给了我一个错误:

but this one give me an error:

friend bool operator> (Para const *p1, Para const *p2){
        return p1->wrt > p2->wrt;
}

<小时>

Error   1   error C2803: 'operator >' must have at least one formal parameter of class type

推荐答案

你的重载 <<之所以有效,是因为它是在 ostream 对象上调用的 (ostream.operator<<()).

Your overloaded << works because it is being called on an ostream object (ostream.operator<<()).

运算符<的指针重载不起作用,因为指针不是类,因此以下内容毫无意义:(const Para*).operator<().

The pointer overload of operator < does not work because a pointer is not a class so the following is meaningless: (const Para*).operator<().

这篇关于如何制作与指针一起使用的重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆