本文共 838 字,大约阅读时间需要 2 分钟。
下面代码实现+、()、=的运算符重载,仅供参考。
#includeclass Body{public: Body(int a = 0):m_a(a) {} ~Body() {}#include 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;}
转载地址:http://dimci.baihongyu.com/