Hi all
Нельзя возвращать ссылку, если возвращаемое значение является локальной переменоой как здесь, потому что локальные переменные уничтожаются после выхода из функции
Код:
Fraction& Fraction::add(Fraction other) {
Fraction fract;
int lcd = lcm(den, other.den);
int quot1 = lcd/den;
int quot2 = lcd/other.den;
fract.set(num * quot1 + other.num * quot2, lcd);
fract.normalize();
return fract;
}
Вот весь код, где возвращается ссылка из локальной переменной:
Код:
#include "stdafx.h"
#include "conio.h"
using namespace std;
class Fraction {
private:
int num, den;
public:
Fraction() {num = 0; den = 0; normalize();}
Fraction(int n, int d) {set(n, d);}
void set(int n, int d) {num = n; den = d; normalize();}
int get_num() {return num;}
int get_den() {return den;}
Fraction add(Fraction other);
Fraction mult(Fraction other);
private:
void normalize();
int gcf(int a, int b);
int lcm(int a, int b);
};
int main(){
Fraction f1, f2;
Fraction f3(1, 2);
cout << "The value of f1 is ";
cout << f1.get_num() << "/";
cout << f1.get_den() << endl;
cout << "The value of f3 is ";
cout << f3.get_num() << "/";
cout << f3.get_den() << endl;
return 0;
}
void Fraction::normalize(){
if (den == 0 || num == 0) {
num = 0;
den = 1;
}
if (den < 0) {
num *= -1;
den *= -1;
}
int n = gcf(num, den);
num = num / n;
den = den / n;
}
int Fraction::gcf(int a, int b) {
if (a % b == 0)
return abs(b);
else
return gcf(b, a % b);
}
int Fraction::lcm(int a, int b){
return (a / gcf(a, b)) * b;
}
Fraction& Fraction::add(Fraction other) {
Fraction fract;
int lcd = lcm(den, other.den);
int quot1 = lcd/den;
int quot2 = lcd/other.den;
fract.set(num * quot1 + other.num * quot2, lcd);
fract.normalize();
return fract;
}
А если эту переменную Fraction fract; объявить глобально ?
Почему так нельзя?
Вот код, где возвращается ссылка для глобальной переменной
Код:
#include "stdafx.h"
#include "conio.h"
using namespace std;
class Fraction {
private:
int num, den;
public:
Fraction() {num = 0; den = 0; normalize();}
Fraction(int n, int d) {set(n, d);}
void set(int n, int d) {num = n; den = d; normalize();}
int get_num() {return num;}
int get_den() {return den;}
Fraction add(Fraction other);
Fraction mult(Fraction other);
private:
void normalize();
int gcf(int a, int b);
int lcm(int a, int b);
};
Fraction fract;
int main(){
Fraction f1, f2;
Fraction f3(1, 2);
cout << "The value of f1 is ";
cout << f1.get_num() << "/";
cout << f1.get_den() << endl;
cout << "The value of f3 is ";
cout << f3.get_num() << "/";
cout << f3.get_den() << endl;
return 0;
}
void Fraction::normalize(){
if (den == 0 || num == 0) {
num = 0;
den = 1;
}
if (den < 0) {
num *= -1;
den *= -1;
}
int n = gcf(num, den);
num = num / n;
den = den / n;
}
int Fraction::gcf(int a, int b) {
if (a % b == 0)
return abs(b);
else
return gcf(b, a % b);
}
int Fraction::lcm(int a, int b){
return (a / gcf(a, b)) * b;
}
Fraction& Fraction::add(Fraction other) {
int lcd = lcm(den, other.den);
int quot1 = lcd/den;
int quot2 = lcd/other.den;
fract.set(num * quot1 + other.num * quot2, lcd);
fract.normalize();
return fract;
}