G
G
Governor2017-10-15 06:13:13
C++ / C#
Governor, 2017-10-15 06:13:13

How to fix a leak?

I'm just learning C++, and I'm currently reading about operator overloading. But then I decided to make an addition operation and realized that when creating the third result object, I simply get lost. I thought somehow through the copy constructor to solve the problem, in general I got confused, I don’t understand anything here is the code, help me:

#include <iostream>
using namespace std;

class obj
{
  int a;
public:
  void show() {cout<<a<<endl;}
  obj(){a=5;}
  obj(int i){a = i;}

  obj operator+(obj xa) //Этот опер-р отдает новый объект, с суммой двух предыдущих.
  {
    return *new obj (this->a + xa.a);
  }
};

int main()
{
setlocale(0,"");
  obj one, two;
  (one + two).show(); Вот он тут отображает результат и просто теряется.
system("pause");
return 0;
}

As usual, they fight with such .. mm. in such situations ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2017-10-15
@Mr-Governor

return *new obj (this->a + xa.a);

Replaced with PS with exceptions figured it out?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question