オブジェクト参照がパラメーターとして渡されると、関数 (pass(sample const &ob1)) スコープの終了後にデストラクタが呼び出されるのはなぜですか? オブジェクト参照を渡しているときに、関数 pass() で新しいオブジェクトを作成するのはなぜですか?
これについて助けてください、メモリダンプエラーが発生しています
#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;
}