i have a little program that tries to do it but fails
class myclass{
public:
int * ptr;
myclass (){
ptr = new int;
}
myclass (const myclass &class_inst){
ptr = new int;
*ptr = *class_inst.ptr;
}
~myclass (){
delete ptr;
}
};
myclass dosomething (myclass a){
// make some changes to a
return a;
}
int main(){
myclass test1;
test1 = dosomething (test1);
return 0;
}
after test1 = dosomething.. is executed, test1's destructor is called. it gets called again at the end of main and that causes a seg fault.
1 way to fix this would be to have dosomething as a member function and use test1.dosomething(). Id like to know what is wrong with the above code.
Thanks!