如果我的 class 正确地管理资源,那么拥有智能指针有什么意义呢?
If my class manages a resource correctly then what is the point in having smart poointers?
我是智能指针的新手,我喜欢将它们用于共享对象的安全性和强大功能...
我有一个问题:如果我的 class 在其构造函数和析构函数中管理资源并应用一些经验法则,例如 Big 5 和 Big 3...我是否仍应使用智能指针?或者我的 class 是他们的替代品。因为正如我在 C++ primer 5 版中读到的那样,智能指针开始解决原始指针面临的问题,例如内存泄漏、双删除指针和访问悬垂指针……我的 class 可以避免这些问题:
class BallGame {
public:
using Resource = int;
BallGame(int);
BallGame(const BallGame&);
BallGame(BallGame&&);
BallGame& operator=(const BallGame&);
BallGame& operator=(BallGame&&);
~BallGame();
private:
Resource m_res;
};
考虑到我的class成员做的是对的,我可以避免智能指针吗?
我想知道在 class.
中我应该使用智能指针而不是管理资源的一些场景
它们真的像 C++ 入门书那样用于 "dumb classes"(class 定义构造函数而不是行为良好的析构函数)。
您的问题可以解读为
If I implement correct semantics for memory ownership of a smart pointer by hand in each class I use, can I avoid using smart pointers?
是的,但是为什么呢?当你需要一个动态数组时,你是否每次都需要手动重新实现基本的 std::vector
语义?我不这么认为。
这是图书馆的目的,避免每次都重新发明轮子。
正如您在书中所读:它们解决了原始指针面临的问题:考虑一下:
int* ptr = new int[1024];
// oh my God! forgot to free it then welcome to memory leak
std::string* pStr = new std::string("Hello");
delete pStr;
//...
// and at some point in the program you think it is time to free pStr which has been already freed (among hundreds of code lines) then welcome to U.B.
std::cout << *pStr << std::endl; // pStr is a dangling pointer. U.B
使用智能指针解决这个问题:
std::unique_ptr<int> upi = std::make_unique(7);
// use upi effectively and don't care about freeing it because it automatically will be freed for you.
您的 class 无法支持的另一个原因是在多个对象之间有效地共享同一个对象。事实上,您可以通过在 class 中应用浅拷贝来实现这一点,但问题是谁将释放该资源,然后您最终会在您可能很小的 class 中实现 shared_ptr 的功能]!
每个新版本的 C++ 都带有新功能和新的强大功能,因此您应该坚持使用这些库,而不是重新发明这些库。如果只是出于某种教育原因,那么练习是可以的。
如果您仔细观察,您会发现真正的程序包含智能指针作为成员,这使您的 class 健壮。
另一个尚未提及的智能指针的好处是,它们可以让阅读代码的人对该对象的生命周期有一个体面的了解。使用原始指针的问题是(尤其是当代码变得更复杂时)可能很难弄清楚谁负责调用对象上的 delete,而如果您有一个唯一的指针,我们会立即知道删除将在删除时自动发生指针超出范围。
我是智能指针的新手,我喜欢将它们用于共享对象的安全性和强大功能...
我有一个问题:如果我的 class 在其构造函数和析构函数中管理资源并应用一些经验法则,例如 Big 5 和 Big 3...我是否仍应使用智能指针?或者我的 class 是他们的替代品。因为正如我在 C++ primer 5 版中读到的那样,智能指针开始解决原始指针面临的问题,例如内存泄漏、双删除指针和访问悬垂指针……我的 class 可以避免这些问题:
class BallGame {
public:
using Resource = int;
BallGame(int);
BallGame(const BallGame&);
BallGame(BallGame&&);
BallGame& operator=(const BallGame&);
BallGame& operator=(BallGame&&);
~BallGame();
private:
Resource m_res;
};
考虑到我的class成员做的是对的,我可以避免智能指针吗?
我想知道在 class.
中我应该使用智能指针而不是管理资源的一些场景
它们真的像 C++ 入门书那样用于 "dumb classes"(class 定义构造函数而不是行为良好的析构函数)。
您的问题可以解读为
If I implement correct semantics for memory ownership of a smart pointer by hand in each class I use, can I avoid using smart pointers?
是的,但是为什么呢?当你需要一个动态数组时,你是否每次都需要手动重新实现基本的 std::vector
语义?我不这么认为。
这是图书馆的目的,避免每次都重新发明轮子。
正如您在书中所读:它们解决了原始指针面临的问题:考虑一下:
int* ptr = new int[1024];
// oh my God! forgot to free it then welcome to memory leak
std::string* pStr = new std::string("Hello");
delete pStr;
//...
// and at some point in the program you think it is time to free pStr which has been already freed (among hundreds of code lines) then welcome to U.B.
std::cout << *pStr << std::endl; // pStr is a dangling pointer. U.B
使用智能指针解决这个问题:
std::unique_ptr<int> upi = std::make_unique(7);
// use upi effectively and don't care about freeing it because it automatically will be freed for you.
您的 class 无法支持的另一个原因是在多个对象之间有效地共享同一个对象。事实上,您可以通过在 class 中应用浅拷贝来实现这一点,但问题是谁将释放该资源,然后您最终会在您可能很小的 class 中实现 shared_ptr 的功能]!
每个新版本的 C++ 都带有新功能和新的强大功能,因此您应该坚持使用这些库,而不是重新发明这些库。如果只是出于某种教育原因,那么练习是可以的。
如果您仔细观察,您会发现真正的程序包含智能指针作为成员,这使您的 class 健壮。
另一个尚未提及的智能指针的好处是,它们可以让阅读代码的人对该对象的生命周期有一个体面的了解。使用原始指针的问题是(尤其是当代码变得更复杂时)可能很难弄清楚谁负责调用对象上的 delete,而如果您有一个唯一的指针,我们会立即知道删除将在删除时自动发生指针超出范围。