我想用我的類的shared_ptr,但由於某種原因,我得到段錯誤。也許我在做一些愚蠢的事情。shared_ptr導致段錯誤
#include <tr1/memory>
#include <iostream>
class Dataset ;
typedef int DataClass;
class Dataset_Impl{
friend class Dataset ;
DataClass *dc;
Dataset_Impl(){dc = new DataClass[10];}
public:
void getSubset(Dataset_Impl* &dObj){
dObj = new Dataset_Impl(); //Causing segfault when using shared_ptr
/*copy subset of 'dc' to dObj->dc and return*/std::cout<<"Copied subset";
}
};
class Dataset{
Dataset_Impl *d;
public:
Dataset(){};
inline void const getSubset(Dataset &dObj) const{d->getSubset(dObj.d);}
};
int main(){
Dataset m1,subset1;std::shared_ptr<Dataset> subset2;
m1.getSubset(subset1);
m1.getSubset((*subset2)); //Causing segfault
}