我创建了自己的智能指针class,我用这个智能指针指向了一个class实例,我怎样才能正确return智能指针
I created my own smart pointer class, I used this smart pointer to point to a class instance, how can I properly return the smart pointer
我是 C++ 的新手,正在尝试实现其中的一些方法。
我创建了一个智能指针class,CarPtr,如下:
template <class Car>
class CarPtr
{
public:
Car *ptr; // Actual pointer
// Constructor
explicit CarPtr(Car *p = nullptr) { ptr = p; }
// Destructor
~CarPtr() { delete(ptr); cout << "CarPtr destructor" << endl;}
// Overloading dereferencing operator
Car &operator *() { return *ptr; }
Car *operator -> () { return ptr; }
};
#endif /* CARPTR_H_ */
然后我创建了 Car 的实例 class 并使用我的智能指针指向它。
CarPtr<Car> carInfoCreation()
{
CarPtr<Car> p(new Car());
int carId;
int carYear;
double carCost;
String carMake;
String carModel;
cout << "please enter the Id for the car" << endl;
cin >> carId;
p.ptr->setId(carId);
cout << "please enter the Year for the car" << endl;
cin >> carYear;
p.ptr->setYear(carYear);
cout << "please enter the Cost for the car" << endl;
cin >> carCost;
p.ptr->setCost(carCost);
cout << "please enter the Make for the car" << endl;
cin >> carMake;
p.ptr->setMake(carMake);
String getMake = p.ptr->getMake();
cout << "please enter the Model for the car" << endl;
cin >> carModel;
p.ptr->setModel(carModel);
String getModel = p.ptr->getModel();
cout << "Car Info: " << endl;
cout << "Car Id: "<< p.ptr->getId() << endl;
cout << "Car Year: "<< p.ptr->getYear() << endl;
cout << "Car Cost: " << p.ptr->getCost() << endl;
cout << "Car Make: " << getMake << endl;
cout << "Car Model: " << getModel << endl;
ofstream myfile;
myfile.open ("Car 1.txt");
myfile << "Car Id: " << carId << endl;
myfile << "Car Year: " << carYear << endl;
myfile << "Car Cost: " << carCost << endl;
myfile << "Car Make: " << carMake << endl;
myfile << "Car Model: " << carModel << endl;
myfile.close();
return p;
}
创建实例后,我希望return智能指针p,并将其放入我创建的智能指针数组中,如下所示:
CarPtr<Car> CarPtrArray[5];
CarPtrArray[0] = carInfoCreation() ;
我运行代码后,实例创建成功,可以看到对应的.txt文件。但在那之后,我将看到 Eclipse 显示的错误:
"xxx.exe 已停止工作
一个问题导致程序停止正常工作。 Windows 将关闭程序并在有可用解决方案时通知您。"
谁能帮我解决这个问题?如果您需要更多代码或相关信息,请告诉我。
非常感谢!
您的 class 需要关注 the Rule of Three/Five/Zero。
否则,return像你一样按值计算一个会导致未定义的行为:当临时 return 对象被销毁时指针将被删除(and/or 本地对象正在 returned),但随后调用代码继续使用指针。
我是 C++ 的新手,正在尝试实现其中的一些方法。
我创建了一个智能指针class,CarPtr,如下:
template <class Car>
class CarPtr
{
public:
Car *ptr; // Actual pointer
// Constructor
explicit CarPtr(Car *p = nullptr) { ptr = p; }
// Destructor
~CarPtr() { delete(ptr); cout << "CarPtr destructor" << endl;}
// Overloading dereferencing operator
Car &operator *() { return *ptr; }
Car *operator -> () { return ptr; }
};
#endif /* CARPTR_H_ */
然后我创建了 Car 的实例 class 并使用我的智能指针指向它。
CarPtr<Car> carInfoCreation()
{
CarPtr<Car> p(new Car());
int carId;
int carYear;
double carCost;
String carMake;
String carModel;
cout << "please enter the Id for the car" << endl;
cin >> carId;
p.ptr->setId(carId);
cout << "please enter the Year for the car" << endl;
cin >> carYear;
p.ptr->setYear(carYear);
cout << "please enter the Cost for the car" << endl;
cin >> carCost;
p.ptr->setCost(carCost);
cout << "please enter the Make for the car" << endl;
cin >> carMake;
p.ptr->setMake(carMake);
String getMake = p.ptr->getMake();
cout << "please enter the Model for the car" << endl;
cin >> carModel;
p.ptr->setModel(carModel);
String getModel = p.ptr->getModel();
cout << "Car Info: " << endl;
cout << "Car Id: "<< p.ptr->getId() << endl;
cout << "Car Year: "<< p.ptr->getYear() << endl;
cout << "Car Cost: " << p.ptr->getCost() << endl;
cout << "Car Make: " << getMake << endl;
cout << "Car Model: " << getModel << endl;
ofstream myfile;
myfile.open ("Car 1.txt");
myfile << "Car Id: " << carId << endl;
myfile << "Car Year: " << carYear << endl;
myfile << "Car Cost: " << carCost << endl;
myfile << "Car Make: " << carMake << endl;
myfile << "Car Model: " << carModel << endl;
myfile.close();
return p;
}
创建实例后,我希望return智能指针p,并将其放入我创建的智能指针数组中,如下所示:
CarPtr<Car> CarPtrArray[5];
CarPtrArray[0] = carInfoCreation() ;
我运行代码后,实例创建成功,可以看到对应的.txt文件。但在那之后,我将看到 Eclipse 显示的错误:
"xxx.exe 已停止工作
一个问题导致程序停止正常工作。 Windows 将关闭程序并在有可用解决方案时通知您。"
谁能帮我解决这个问题?如果您需要更多代码或相关信息,请告诉我。
非常感谢!
您的 class 需要关注 the Rule of Three/Five/Zero。
否则,return像你一样按值计算一个会导致未定义的行为:当临时 return 对象被销毁时指针将被删除(and/or 本地对象正在 returned),但随后调用代码继续使用指针。