C++官方文档-运算符重载
2024-08-27 06:29:03
#include <iostream>
using namespace std; class CVector
{
public:
int x, y;
CVector()
: x(), y()
{
}
CVector(int a, int b)
: x(a), y(b)
{
}
;
CVector operator +(const CVector&);
}; //+号是双目运算,这个成员函数的形式
CVector CVector::operator +(const CVector& param)
{
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return temp;
} //+号的非成员函数形式
CVector operator+(const CVector& lhs, const CVector& rhs)
{
cout<<"no member function"<<endl;
CVector temp;
temp.x = lhs.x + rhs.x;
temp.y = lhs.y + rhs.y;
return temp;
}
/**
*运算符重载表,注意成员函数和非成员函数的区别
* Expression Operator Member function Non-member function
* @a + - * & ! ~ ++ -- A::operator@() operator@(A)
* a@ ++ -- A::operator@(int) operator@(A,int)
* a@b + - * / % ^ & | < > == != <= >= << >> && || , A::operator@(B) operator@(A,B)
* a@b = += -= *= /= %= ^= &= |= <<= >>= [] A::operator@(B) -
* a(b,c...) () A::operator()(B,C...) -
* a->b -> A::operator->() -
* (TYPE) a TYPE A::operator TYPE() -
*/ int main()
{
CVector foo(, );
CVector bar(, );
CVector result;
CVector result2 = foo.operator +(bar);
result = foo + bar;
CVector result3 = foo+bar;
cout << result.x << ',' << result.y << endl;
cout << result2.x << ',' << result2.y << endl;
cout << result3.x << ',' << result3.y << endl;
return ;
}
最新文章
- 从零开始编写自己的C#框架——框架学习补充说明
- c 算牌器代码
- 如何牢记C/C++中const的用法?
- shell 显示字体颜色
- linux笔记:shell基础-概述和脚本执行方式
- malloc函数详解
- SAP和Java系统的Webservice实例
- javascript单元测试工具
- Shell学习笔记 - Shell变量
- 在Xcode6中找回失去的模板
- Android开发UI之动画侦听
- 玩转Android之数据库框架greenDAO3.0使用指南
- HDU 4276 The Ghost Blows Light(树形)
- 使用Pycharm 安装三方库
- flash Ane
- delegate vs event
- PyCOn2013大会笔记
- Python实现身份证号码有效性验证
- 4337: BJOI2015 树的同构
- python 彩色日志配置