Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

c++ - 析构函数,返回临时对象(Destructor , Returning temp object)

I do not understand what is the problem in returning temp object.

(我不明白返回临时对象有什么问题。)

If I do not use destructor then everything is fine.

(如果我不使用析构函数,那么一切都很好。)

But with destructor it creates problem.coefficients for polynomial 1 and 2 are printed correctly.polynomial 3 also.

(但是使用析构函数会产生问题。多项式1和2的系数正确打印。多项式3也是如此。)

But after addition coeff[1] and coeff[2] of polynomial 3 didn't give correct value.Can anyone help?

(但是多项式3的coeff [1]和coeff [2]相加后没有给出正确的值。有人可以帮忙吗?)

#include<iostream>

using namespace std;

class poly{
public:
float* coeff;
int degree;
int arr_size;
/*default constructor*/
poly(){
    coeff = new float [11];
    arr_size = 10;
    for(int j=0;j<=arr_size;j++)
        coeff[j] = 10;
    cout << "Object created using default constructor..." << endl;
}

 poly(poly &p){
    arr_size = p.arr_size;
    coeff = new float[arr_size+1];
    for(int j=0;j<=arr_size;j++)
        coeff[j] = p.coeff[j];
    cout << "Copy constructor called......"<<endl;
}

~poly(){
    if(coeff){
        delete [] coeff;
        coeff = NULL;
        cout << "Destructor Msg:: Allocation free!!" << endl;
    }
}

void show();
void setCoeff();
poly operator+ (poly);};
 /*Show all coefficiens of a ploynomial*/
 void poly :: show(){
 for(int j=0; j<=arr_size; j++)
    cout << "coeff[" << j <<"]:"<< coeff[j]<< endl;
}



   /*To set a specific coefficient in the polynomial*/
 void poly :: setCoeff(){
 int i;
 again: cout << "Enter degree of coefficient you want to set: ";
 cin >> i;
 if(i>arr_size || i<0){
     cout << "!! Enter appropriate value." << endl;
    goto again;
}
cout << "Enter new value: ";
cin >> coeff[i];
}

poly poly :: operator+ (poly p){
poly temp;
for(int i=0;i<=arr_size;i++)
    temp.coeff[i] = coeff[i] + p.coeff[i];
    return temp; //I think Problem in this line 
 } 





int main(){
    cout << "***********  WELCOME  ***********" << endl;
    poly p[3];
    p[2] = p[0] + p[1];
    p[2].show();
    cout << "Thank You!!!" << endl;
    return 0;
}
  ask by user5347919 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You did not define an assignment operator, so when you do p[2] = p[0] + p[1] , the default assignment operator is used, which assigns p[2].coeff to point to the same array as the coeff of the temporary object created by the addition.

(您没有定义赋值运算符,因此当您执行p[2] = p[0] + p[1] ,将使用默认的赋值运算符,它将p[2].coeff指向与数组相同的数组。加法创建的临时对象的coeff 。)

So when the temporary object is destroyed, its destructor deletes the array and p[2].coeff is now an invalid pointer.

(因此,当临时对象被销毁时,其析构函数将删除该数组,并且p[2].coeff现在是无效的指针。)

Therefore accessing it will lead to undefined behaviour.

(因此,访问它会导致不确定的行为。)

Also a copy constructor should take a const reference as its argument.

(同样,复制构造函数应将const引用作为其参数。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...