|
-
June 18th, 2011, 08:46 PM
#1
need little help of bug fixers.
here is a code which add data of two classes using operator overloading. But it hangs due to double calling of destructor for same pointer. But i think it should not so.please tell me how to remove this problem from this code.(error is most probably in red region)
Code:
#include<iostream>
using namespace std;
class x
{
private:
int *******;
int* pointer;
public:
x(int,int);
x(const x&);
~x();
x operator+(x x1) const;
void get() const;
int geti()
{
return *******;
}
int getp()
{
return (*pointer);
}
void change(int a,int b)
{
*******=a;
pointer=new int(b);
cout<<"\t\t"<<pointer<<endl;
cout<<"change"<<endl;
}
};
x::x(int *******x=1,int pointerx=1)
{
try
{
*******=*******x;
pointer=new int(pointerx);
cout<<"constrctor"<<endl;
}
catch(bad_alloc &ex)
{
cout<<"ERROR "<<ex.what()<<endl;
}
}
x::x(const x & x5 )
{
*******=x5.*******;
int*tmp=new int(*(x5.pointer));
pointer=tmp;
cout<<"copy constructor"<<endl;
}
x::~x()
{
cout<<"destructor ";
cout<<*******<<endl;
delete pointer;
}
void x::get() const
{
cout<<*******
<<endl
<<*pointer
<<endl;
}
/*x x::operator+(x xn) const
{
cout<<"we get this data\n";
cout<<*******<<endl<<*pointer<<endl;
xn.get();
cout<<"that's all\n";
int tmp*******=xn.*******+*******;
int tmppointer=((*(xn.pointer))+(*(pointer)));
x *returnx=new x(tmp*******,tmppointer);
cout<<"addition coplete"<<endl;
return (*returnx);
}*/
x operator+(x,x);
int main()
{
x x1(1,1);
x x2(10,20);
x1.get();
x2.get();
x x3;
x3=x1+x2;
x1.get();
x2.get();
x3.get();
cout<<"sum of 2 and 1 is "<<2+1<<endl;
return 0;
}
x operator+(x x1f,x x2f)
{
cout<<"add function..."<<endl;
int xir=x1f.geti()+x2f.geti();
int xprv=x1f.getp()+x2f.getp();
cout<<xir<<"\t"<<xprv<<endl;
x xr(xir,xprv);
cout<<"returning..."<<endl;
return xr;
}
[/color]
//why these guys(codeguru mangers)
put ******in place of int i ger (remove space )
this is not abusive word)???????all the places where you see
****** in this post i write their inti ge r(without space)
as you can see their are two overload for addition but one is hidden. I hide that because in returning of that function destructor is called for all the class x variable and all pointer loose their data.
If i call any class function(operator+ of class x) using class variable then it's destructor(main caller like x1.get() not destructor) should not called at the last of that function but in case of operator+ (of class x) this happen destructor is called for all.why. Is it done in special cases like operator overloading???
There should not any error in second operator+(after main function). Error is due to calling of destructor of xprv is deleted at the end of this function but it should not matter pointer value in xr because this is created dynamically in constructor.(but i am wrong why???)
thanks to all helper.
Last edited by vkash; June 18th, 2011 at 11:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|