When the class operator "+" is overloaded, I create a new object in dynamic memory and immediately return it. Where and how in the program do I need to delete it?

#include "pch.h" #include <iostream> #include <cmath> #include <ctime> using namespace std; #define N 10 class Fraction { private: int chys, zn; public: Fraction() { this->chys = 0; this->zn = 0; } Fraction(int chys, int zn) { this->chys = chys; this->zn = zn; } Fraction(const Fraction &obj) { this->chys = obj.chys; this->zn = obj.zn; } int GetChys() { return chys; } int GetZn() { return zn; } void setFraction(int x, int y) { chys = x; zn = y; } int CommonZn(Fraction *B) { int q = 0; int i = 0; for (i = 1; q != 1; i++) { if ((i % this->zn == 0) && (i % B->zn == 0)) { q = 1; this->chys *= (i / this->zn); B->chys *= (i / B->zn); } } this->zn = i - 1; B->zn = i - 1; return i - 1; } int skor(int *chys, int *zn) { int i = 0, b = 0; if (*chys > *zn) { b = *chys; } else (b = *zn); for (i = b; i > 0; i--) { if (*chys%i == 0 && *zn%i == 0) { *chys = *chys / i; *zn = *zn / i; return 0; } } } void comperizon(Fraction *A, Fraction *B) { if (A->chys > B->chys) { cout << endl; cout << "Перший дріб більший за другий" << endl; } else if (A->chys == B->chys) { cout << endl; cout << "Дроби однакові" << endl; } else if (A->chys < B->chys) { cout << endl; cout << "Другий дріб більший за перший" << endl; } } void addition(Fraction *A, Fraction *B) { this->chys = A->chys + B->chys; skor(&this->chys, &this->zn); cout << "\nСума дробів = "; cout << *this; cout << endl; } void subtraction(Fraction *A, Fraction *B) { this->chys = A->chys - B->chys; skor(&this->chys, &this->zn); cout << "\nРізниця дробів = "; cout << *this; cout << endl; } void multiplication(Fraction *A, Fraction *B) { this->chys = (A->chys * B->chys); this->zn = (A->zn * B->zn); skor(&this->chys, &this->zn); cout << "\nДобуток дробів = "; cout << *this; cout << endl; } void division(Fraction *A, Fraction *B) { this->chys = (A->chys * B->zn); this->zn = (A->zn * B->chys); skor(&this->chys, &this->zn); cout << "\nЧастка дробів = "; cout << *this; cout << endl; } Fraction const& operator=(Fraction &second) { chys = second.chys; zn = second.zn; return *this; } bool operator == (Fraction &obj) { this->CommonZn(&obj); return (this->chys == obj.chys); } bool operator >= (Fraction &obj) { this->CommonZn(&obj); return (this->chys >= obj.chys); } bool operator > (Fraction &obj) { this->CommonZn(&obj); return ((double)this->chys / this->zn > (double)obj.chys / obj.zn); } bool operator < (Fraction &obj) { this->CommonZn(&obj); return ((double)this->chys / this->zn < (double)obj.chys / obj.zn); } bool operator <= (Fraction &obj) { this->CommonZn(&obj); return (this->chys <= obj.chys); } friend Fraction& operator +(Fraction &first, Fraction &second); friend istream &operator >> (istream &in, Fraction &fraction); friend ostream &operator << (ostream &out, const Fraction &fr); ~Fraction() {} }; Fraction& operator +(Fraction &first, Fraction &second) { first.CommonZn(&second); return *new Fraction(first.chys + second.chys, first.zn); } istream &operator >> (istream &in, Fraction &fraction) { in >> fraction.chys; in.ignore(1); in >> fraction.zn; return in; } ostream &operator << (ostream &out, const Fraction &fr) { out << fr.chys << "/" << fr.zn; return out; } void Sort(Fraction *FrArray[]); //double avarage(Fraction FrArray[]); int main() { system("chcp 1251"); system("color f0"); system("cls"); srand(time(nullptr)); int chys1, zn1, chys2, zn2; Fraction *PtrFrArray[N]; for (int i = 0; i < N; i++) { PtrFrArray[i] = new Fraction(1 + rand() % 10, 1 + rand() % 10); cout << *PtrFrArray[i] << endl; } Sort(PtrFrArray); cout << endl << "Сортований масив: " << endl; for (int i = 0; i < N; ++i) { cout << *PtrFrArray[i] << endl; } cout << endl; double avar = 0.0; for (int i = 0; i < N; i++) { avar += (double) (*PtrFrArray[i]).GetChys() / (*PtrFrArray[i]).GetZn(); } cout << "Середнє значення масиву = " << avar/N << endl << endl; for (int i = 0; i < N; i++) { delete PtrFrArray[i]; } Fraction *A = new Fraction(); cout << "\nПерший дріб = " << endl; cin >> *A; Fraction *B = new Fraction(); cout << "\nДругий дріб = " << endl; cin >> *B; Fraction *C = new Fraction(*A); cout << "Enter C " << endl; cin >> *C; if (*A > *C && *C < *B) cout << "Успішне порівняння" << endl; else cout << "Помилка" << endl; cout << "A+B+C = " << *A + *B + *C; delete A; delete B; delete C; getchar(); getchar(); return 0; } void Sort(Fraction *FrArray[]) { Fraction changer; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (*FrArray[i] > *FrArray[j]) { changer = *FrArray[i]; *FrArray[i] = *FrArray[j]; *FrArray[j] = changer; } } } } /*double avarage(Fraction *FrArray[]) { double avar = 0.0; for (int i = 0; i < N; i++) { avar = avar + (double)(*FrArray[i]).GetChys()/ (*FrArray[i]).GetZn(); } return(avar/N); }*/ 

Closed due to the fact that the essence of the question is incomprehensible by the participants αλεχολυτ , 0xdb , LFC , freim , Kosta B. 13 Apr at 19:11 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Use smart pointers. Where you are sure that you will no longer refer to this object there and call the destructor. The truth is better use std :: unique_ptr, a great example to understand that smart pointers are beautiful - jNX

2 answers 2

Best of all - no way. Stop doing such rubbish as creating the result of an operator in dynamic memory. And constant correctness should also be corrected (everywhere, not only here)

 Fraction operator +(const Fraction &first, const Fraction &second) { first.CommonZn(&second); return Fraction(first.chys + second.chys, first.zn); } 

And why CommonZn accepts its operand as Fraction *B , and not const Fraction &B - is also not clear.


Aah, I see! Your CommonZn modifies its right operand! That is, even your + operator modifies its operands (!). An addition operator that modifies its operands (even if the actual value of the fraction does not change) is unacceptable. It is necessary to redo much more. for example

 Fraction operator +(const Fraction &first, const Fraction &second) { Fraction first_common = first, second_common = second; first_common.CommonZn(second_common); return Fraction(first_common.chys + second_common.chys, first_common.zn); } 

    Bad decision.

    Return not the link, but the object -

     Fraction operator +(Fraction &first, Fraction &second) { first.CommonZn(&second); return Fraction(first.chys + second.chys, first.zn); } 

    So it is much better.

    And yet - I would replace your designers with

     Fraction(int chys = 0, int zn = 1):chys(chys),zn(zn) {} Fraction(const Fraction &obj):chys(obj.chys),zn(obj.zn) {} 

    well and still on trifles would remake ... But this is another question ...