TLDN'R:您应该在存储库中使用std::shared\u ptr的向量,或者更改
getNewCar
返回引用。
我对您的代码做了一些修改,使输出更加详细:
#include <memory>
#include <iostream>
#include <vector>
class Car
{
public:
Car()
{
std::cout << "Car ctor @" << this << std::endl;
}
Car(const Car& car)
{
std::cout << "Car copy ctor @" << this << " from " << &car << std::endl;
}
Car& operator=(const Car& car)
{
std::cout << "Car assign operator @" << this << std::endl;
}
~Car()
{
std::cout << "Car dtor @" << this << std::endl;
}
};
class Repository
{
std::vector<Car> collection;
public:
std::shared_ptr<Car> getNewCar()
{
Car newType;
collection.push_back(newType);
return std::shared_ptr<Car>(&collection.back());
}
};
int main()
{
Repository rep;
std::shared_ptr<Car> myCar = rep.getNewCar();
return 0;
}
代码结果为:
Car ctor @0x7ffe35557217
Car copy ctor @0x25b6030 from 0x7ffe35557217
Car dtor @0x7ffe35557217
Car dtor @0x25b6030
Car dtor @0x25b6030
这个
std::vector
您在存储库中使用的对象在被销毁时负责删除其所有对象。最后一个指向某个对象的“std::shared\u ptr”也负责这样做。
直线:
return shared_ptr<Type>(&collection.back());
获取当前所属对象的地址
标准::矢量
并创建
std::shared_ptr
使用指针。这
std::shared\u ptr
认为它是第一个跟踪对象的智能指针。因此,当它被销毁时(或者如果您制作了一些,则为其最后一个副本),将调用该对象析构函数。
因此,如果您认为汽车的寿命将比其来源的存储库长:
class Repository
{
std::vector<std::shared_ptr<Car>> collection;
public:
std::shared_ptr<Car> getNewCar()
{
collection.push_back(std::shared_ptr<Car>(new Car()));
return collection.back();
}
};
class Repository
{
std::vector<Car> collection;
public:
Car& getNewCar()
{
Car newCar;
collection.push_back(newCar);
return collection.back();
}
};