加号运算符重载

#include <iostream>

class Person
{
public:
   int age_get()
   {
      return m_age;
   }

   void age_set(int age)
   {
      m_age = age;
   }

   Person operator+(Person &person)
   {
      Person p;
      p.m_age = this->m_age + person.m_age;
      return p;
   }

private:
   int m_age;
};

int main()
{
   Person p1;
   p1.age_set(10);

   Person p2;
   p2.age_set(20);

   std::cout << (p1 + p2).age_get() << std::endl;

   return 0;
}
$ ./a.out        
30
posted @ 2022-06-10 21:38  thomas_blog  阅读(20)  评论(0编辑  收藏  举报