C++中运算符重载
2016-01-15 17:23
337 查看
下面代码实现+、()、=的运算符重载,仅供参考。
#include <iostream> class Body{ public: Body(int a = 0):m_a(a) {} ~Body() {} #include <iostream> class Body{ public: Body(int a = 0):m_a(a) {} ~Body() {} Body(const Body& a) { this -> m_a = a.m_a; } // +运算符重载 Body operator+(const Body& a) { Body bd; bd.m_a = this -> m_a + a.m_a; return bd; } // ()运算符重载 int operator()(int i) { return this -> m_a + i; } // =运算符重载 Body& operator=(const Body& a) { this -> m_a = a.m_a; return *this; } int get() { return m_a; } private: int m_a; }; int main(int argc, char** args) { Body a(1); Body b(100); Body result = a = b; std::cout << "a: " << result.get() << std::endl; std::cout << (a + b).get() << std::endl; std::cout << a(999) << std::endl; return 0; }
相关文章推荐
- c语言 数组名是常量指针
- MFC中ActiveX控件的使用
- C\C++ 使用do{...}while(0)进行宏定义的好处!!!
- 线程池的实现(c语言)
- 详解C++编程中标记语句与复合语句的写法
- 杨辉三角 C语言
- 魔方阵的实现 C语言
- C++ Primer 5th Chapter 8 学习笔记
- C++运行时获取类型信息的type_info类与bad_typeid异常
- C语言中的常用变量
- C++中关于拷贝构造函数
- Go与C语言的互操作 cgo
- C语言之括号匹配
- C\C++ windows 绕过strptime
- 解析C++编程中的bad_cast异常
- C++ Primer 5th Chapter 7 学习笔记
- C++ Primer 5th Chapter 6 学习笔记
- 详解C++中const_cast与reinterpret_cast运算符的用法
- c++面向对象基础
- C++多态实现及原理