c/c++语言开发共享C++运算符重载

1.重载的意义:类之间的运算。 2.示例: <1>类中定义 1 bool operator==(const class& p) 2 { 3 if(this->x==p.x) return true; 4 else return false; 5 } <2>类外定义(左操作数的参数必须显示指定) bo …

1.重载的意义:类之间的运算。

2.示例:

<1>类中定义

1 bool operator==(const class& p) 2 { 3     if(this->x==p.x) return true; 4     else return false; 5 }

 

<2>类外定义(左操作数的参数必须显示指定)

bool operator==(class const& p1, class const& p2) {       if(p1.x==p2.x) return true;       else return false;    }

注:class是类名,不是class关键字(笔误

<3>注意

c++要求’=’、'[]’、'()’、’->’操作符必须被定义为类的成员操作符,把这些操作符通过全局函数进行重载时会出现编译错误。

 

其余重载运算符的例子以后再更。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/600236.html

(0)
上一篇 2021年5月9日
下一篇 2021年5月9日

精彩推荐