c++运算符重载问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014595589/article/details/82750459
#include <iostream>
using namespace std;
class R;
const R operator+(const R &a,const R &b);
//不加编译器会弹出internal error
class R
{
   private:
      int l;             
      int r;           
   public:
	  R(){}
      R(int l,int r){
         this->l=l;
		 this->r=r;
      }

	  R(const R &m){
	      this->l=m.l*2;
		  this->r=m.r;
	  }
	  
      void dis(){
		  cout<<this->l<<" "<<this->r<<endl;
	  }
      const R &operator=(const R &m){
	     this->l=m.l*3;
		 this->r=m.r;
		 return *this;
	  }
      friend const R operator+(const R &a,const R &b)  
      {
         R res(a.l+b.l,a.r+b.r);
         return res;
      }
	  //去掉& 加friend 加声明

};
int main()
{
   R x(1,2);
   x.dis();
   R y(3,4);
   y.dis();
   R z=x+y;
   z.dis();
   z=x+y;
   z.dis();
   return 0;
}

operator++()  相当于++i;

operator++(int)相当于 i++;

猜你喜欢

转载自blog.csdn.net/u014595589/article/details/82750459
今日推荐