Why is the destructor being called after function (pass(sample const &ob1)) scope ends, when object reference is passed as parameter? Why is it creating a new object in function pass(), while we are passing an object reference?
Help me on this, I'm getting memory dump error
#include<iostream>
using namespace std;
class sample
{
public:
int *ptr;
sample()
{
cout<<"this is default constructor & addr "<<this<<endl;
}
sample(int i)
{
cout<<"this is single parameter constructor & addr "<<this<<endl;
ptr=new int[i];
}
void disp()
{
cout<<"hello \n";
}
~sample()
{
cout<<"destructor & addr "<<this;
delete ptr;
}
};
sample pass(sample const& ob1)
{
for(int i=0;i<5;i++)
ob1.ptr[i]=10;
return ob1;
}
int main()
{
sample obj(5);
sample copy;
cout<<"before calling \n";
obj.disp();
pass(obj);
copy.disp();
cout<<"after calling \n";
return 0;
}