C++操作符重载
来源:互联网 发布:两游记配音软件 编辑:程序博客网 时间:2024/06/05 19:11
内容待完善。。。
1. 示例代码
#include <iostream>class Foo {public: operator double() { std::cout << "double()" << std::endl; return 5.0; } operator int() { std::cout << "int()" << std::endl; return 3; }};int main(){ Foo foo; // calling the implicit ctor double d = foo; int i = foo; //float f = foo; // error C2440 std::cout << "d:" << d <<std::endl; std::cout << "i:" << i <<std::endl; return 0;}
2. 运行效果
double()int()d:5i:3
3. 另一个例子
class XYZ {public: XYZ(); XYZ(double _x, double _y, double _z):x(_x), y(_y), z(_z){} XYZ(const XYZ &a); XYZ& operator=(const XYZ& rhs); XYZ& operator+=(const XYZ& rhs);private: double x; double y; double z; friend XYZ operator+(XYZ& a, XYZ &b);};XYZ::XYZ():x(0), y(0), z(0){}XYZ::XYZ(const XYZ &a): x(a.x), y(a.y), z(a.z){}XYZ& XYZ::operator=(const XYZ &rhs){ if (this == &rhs) return *this; x = rhs.x; y = rhs.y; z = rhs.z; return *this;}XYZ& XYZ::operator+=(const XYZ& rhs){ this->x += rhs.x; this->y += rhs.y; this->z += rhs.z; return *this;}XYZ operator+(XYZ& a, XYZ &b){ XYZ temp; temp.x = a.x + b.x; temp.y = a.y + b.y; temp.z = a.z + b.z; return temp;}int _tmain(int argc, _TCHAR* argv[]){ XYZ a(1,2,3); XYZ b = a; XYZ c; c = a + b; c += a;return 0;}
0 0
- C++【操作符重载】
- c++::操作符重载
- C++:操作符重载
- 操作符重载(C++)
- C++-重载操作符<< >> = + - * /
- c++_操作符重载
- C/C++: 操作符重载案例
- C++操作符重载专题(C++)
- 类型转换和操作符重载 (c#)
- C++——操作符重载
- 重载操作符 (c++primer 4e)
- 【C++】 重载操作符类编程
- 自定义数据类型,操作符重载实例[C#]
- C++primer读书笔记8-重载操作符
- C/C++ 之 操作符重载
- [c++]实例-类array,重载操作符
- C++,重载输出操作符<<, cout.operator()
- c++string类的操作符重载
- java、oracle、jsp中string与date与sql.date的转换
- nginx常用负载方式
- PCIE
- 第九章
- SQL Server 2008 Windows身份验证改为混合模式身份验证
- C++操作符重载
- MFC,VC++计算器小程序
- Web worker耗尽原因定位
- C和C++的struct区别
- iOS周刊 第三期
- Tomcat部署web项目
- RNote__Reshape2
- GridView实现超链接列和图片列,跳转页面并传递参数
- ios周刊 第四期