博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++中运算符重载
阅读量:4058 次
发布时间:2019-05-25

本文共 838 字,大约阅读时间需要 2 分钟。

下面代码实现+、()、=的运算符重载,仅供参考。

#include 
class 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/

你可能感兴趣的文章
VS2005 wince6.0 environment
查看>>
Wince6.0 目录结构
查看>>
BSP_WINCE_ARM_A8_User_Guide
查看>>
Catalog Item & Build
查看>>
AS提交代码到gitee
查看>>
ov2656
查看>>
android 休眠唤醒
查看>>
AT指令
查看>>
ov5640 regs
查看>>
Wince6.0 添加中文支持
查看>>
offline install deb software
查看>>
ubuntu12.04 64bit JB4.2.2
查看>>
11月前做好准备了吗?
查看>>
十月,广州
查看>>
就这样过了一个星期
查看>>
这两天在发呆
查看>>
百年暨南
查看>>
test
查看>>
数据库笔试题
查看>>
chances!I would let it go any more!
查看>>