将指针传递给具有私有 *tor 的类是否危险?
如果我有一个带有私有构造函数和析构函数的类,将经典的 C 风格指针传递给实例(与共享指针相反)是否仍然危险?是否存在内存泄漏的情况? 我的程序中实例的生命周期由友元工厂类专门管理(该类还使用私有自定义删除器以允许管理器类在内部使用shared_ptr对象)。
If I have a class with private constructors and destructors, is it still dangerous to pass classic c-style pointers to instances (as in opposed to shared_ptr)? Are there any situations where memory could leak?
The lifetime of instances in my program is managed by a friend factory class exclusively (which also uses a private custom deleter to allow shared_ptr objects to be used internally by the manager class).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已经拥有指针,则构造函数根本不重要。至于析构函数 - 如果您有一个私有析构函数,您将无法
删除
is,而且我相信,这也会阻止您实例化shared_ptr
(正如 dandrestor 指出的那样 - 除非您提供自定义删除器,它不仅可以与shared_ptr
一起使用)。所有限制都是在编译时施加的,因此,如果您由于隐私问题而未能释放内存,您将在编译时收到通知。
Constructors do not matter at all if you already have the pointer. As for destructors — if you have a private one, you won't be able to
delete
is and, I believe, that will also prevent you from instantiatingshared_ptr
(as dandrestor pointed out — unless you provide a custom deleter, which you can use not only withshared_ptr
).All the limitations are imposed at compile-time, so if you fail to free your memory because of privacy issues you will be notified at compile-time.