#include "stdafx.h"
#include <iostream>
#include <string>
//
//函数重载
//

namespace operator_test
{
//
//先定义一个类
//
class CObject
{
friend std::ostream& operator<<(std::ostream& out, CObject& object);
public:
CObject(int a) : m_a(a)
{

}

int Get()
{
return m_a;
}

CObject& operator+(CObject& obj2)
{
this->m_a += obj2.Get();
return *this;
}
private:
int m_a;
};


std::ostream& operator<<(std::ostream& out, CObject& object)
{
out << object.m_a;
return out;
}
}

void test_operator_test()
{
operator_test::CObject obj1(10);
operator_test::CObject obj2(20);

//
//返回值,把int operator+(CObject& obj2)改成 CObject& operator+(CObject& obj2)
//

std::cout << obj1 + obj2 << std::endl;

//std::cout << obj1 + obj2返回一个std::ostream&,接着往这个返回的ostream写入std::endl; 
}

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2022-01-09
  • 2021-09-28
  • 2021-11-17
猜你喜欢
  • 2021-10-04
  • 2021-12-11
  • 2021-06-14
  • 2021-05-26
  • 2021-05-31
  • 2022-12-23
  • 2022-01-19
相关资源
相似解决方案