当前位置:网站首页>Operator overloading
Operator overloading
2022-07-29 07:56:00 【A tong Muyou】
+,- Operator overloading
You need to access private functions , It can be placed in a class , You can also use friend functions , take operator+ Treat as friend .
com com::operator+(com &obj)
{
com t;
t.m_a = obj.m_a + m_a;
t.m_b = obj.m_b + m_b;
return t;
}Operator overload rule
1. Cannot overload : Scope qualifiers (::), Ternary operator (? :), comma (,),(.*),(sizeof)
2. Overloading cannot change priority and associativity
3. Its operand cannot be changed
4. Cannot have default parameters
Reload step
1. Write the function name
2. Write formal parameters according to operands
3. Write out the return value type according to the usage scenario
4. Operator overloading cannot have default parameters , Otherwise, the operator operands will be changed
Overload output , Input operator
Need to overload outside the class , Because there is ostream And istream
#include <iostream>
using namespace std;
class com
{
friend ostream& operator<<(ostream &out, const com &obj);
private:
int m_a;
int m_b;
public:
com():m_a(1), m_b(2)
{
}
com(int a, int b)
{
m_a = a;
m_b = b;
}
void print()
{
cout << m_a << " + " << m_b << endl;
}
void print1()
{
cout << m_a << " - " << m_b << endl;
}
com operator++();
com operator++(int);
bool operator&&(com& b);
com operator+(com &obj);
com operator-(com &obj);
};
com com::operator-(com &obj)
{
com t;
t.m_a = m_a - obj.m_a;
t.m_b = m_b - obj.m_b;
return t;
}
com com::operator+(com &obj)
{
com t;
t.m_a = obj.m_a + m_a;
t.m_b = obj.m_b + m_b;
return t;
}
bool com::operator&&(com& b)
{
if(m_a == 0 && m_b == 0)
return false;
else
if(b.m_a == 0 && b.m_b)
return false;
else
return true;
}
com com::operator++()
{
this->m_a++;
this->m_b++;
return *this;
}
com com::operator++(int)
{
com t = *this;
m_a++;
m_b++;
return t;
}
ostream& operator<<(ostream &out, const com &obj)
{
out << obj.m_a << " " << obj.m_b;
return out;
}
int main(int argc, char *argv[])
{
com c1(2,0);
com c2(0,1);
com c3(0,0);
// c1++;
// ++c1;
// cout <<++c1<< endl;
if(c3 && (c2.operator+(c1)))
{
cout << "ss" << endl;
}
else
{
cout << "frue" << endl;
}
}
Overloaded logical operators
Can overload , But the short circuit principle cannot be realized
边栏推荐
- Tcp/ip five layer reference model and corresponding typical devices and IPv6
- MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?
- Multi thread shopping
- JVM garbage collection mechanism (GC)
- [cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M
- 工业互联网行至深水区,落地的路要怎么走?
- IonIcons图标大全
- Technology sharing | quick intercom integrated dispatching system
- Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
- 207. Curriculum
猜你喜欢

Dynamic Thresholds Buffer Management in a Shared Buffer Packet Switch论文总结

《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》

Use custom annotations to verify the size of the list

The smallest positive number that a subset of an array cannot accumulate

@JsonSerialize注解的使用
功能自动化测试实施的原则以及方法有哪些?

Multi thread shopping

2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"

Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
![[lecture notes] how to do in-depth learning in poor data?](/img/7d/5767c078600bd88b7d2146069f4f40.jpg)
[lecture notes] how to do in-depth learning in poor data?
随机推荐
在js中,0表示false,非0表示true
Cfdiv1+2-bash and a high math puzzle- (gcd+ summary of segment tree single point interval maintenance)
20 hacker artifacts
MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?
How to get to the deep-water area when the industrial Internet goes?
[freeze electron microscope] analysis of the source code of the subtomogram alignment function of relion4.0 (for self use)
Sqlmap (SQL injection automation tool)
[skill accumulation] common expressions when writing emails
207. Curriculum
MySQL 45 讲 | 07 行锁功过:怎么减少行锁对性能的影响?
2022 Shenzhen Cup Title A: get rid of "scream effect" and "echo room effect" and get out of the "information cocoon room"
JVM garbage collection mechanism (GC)
[cryoelectron microscope | paper reading] a feature guided, focused 3D signal permutation method for subtogram averaging
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
The new colleague wrote a few pieces of code, broke the system, and was blasted by the boss!
Postman interface test | JS script blocking sleep and non blocking sleep
Record of problems caused by PIP upgrade damage
Autojs微信研究:微信自动发送信息机器人最终成品(有效果演示)
Cross domain problems when downloading webapi interface files
技术分享| 快对讲综合调度系统