Tag: smart pointers

使用类成员的智能指针

我无法理解智能指针在C ++ 11中作为类成员的用法。 我读了很多关于智能指针的信息,我想我明白unique_ptr和shared_ptr / weak_ptr如何工作的。 我不明白的是真正的用法。 似乎每个人都推荐使用unique_ptr作为几乎所有的时间。 但是,我将如何实现这样的东西: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device = device; } Device *getDevice() { return device; } }; int main() { Device *device = new Device(); Settings settings(device); // … Device *myDevice = settings.getDevice(); // do something with myDevice… } […]