C++和智能指针 - 在这种情况下智能指针有何帮助?
很遗憾的是,我在实际开发中还没有机会使用智能指针(主管认为太“复杂”,浪费时间)。但是,我计划将它们用于我自己的东西...
我遇到过在模块完成后或加载新数据时取消初始化的情况。当我使用指针时,我发现我的代码中充斥着检查null 这样的...
// TODO: Reset all opened windows
// Deinit track result player
if (trackResultPlayer_)
trackResultPlayer_->reset();
// disconnect track result player
disconnect(trackResultPlayer_);
disconnect(trackResultAnimator_);
}
if (videoPlayerWindow_)
{
videoPlayerWindow_->reset();
// Disconnect the video player window from source movie data
disconnect(videoPlayerWindow_);
}
// Disconnect this module from its children as they would be connected again
disconnect(this);
如果我要使用智能指针而不是原始指针,如何缓解这个问题?
Much to my shame, I haven't had the chance to use smart pointers in actual development (the supervisior deems it too 'complex' and a waste of time). However, I planned to use them for my own stuff...
I have situations regarding de-initing a module after they are done, or when new data is loaded in. As I am using pointers, I find my code littered with check for null such as this...
// TODO: Reset all opened windows
// Deinit track result player
if (trackResultPlayer_)
trackResultPlayer_->reset();
// disconnect track result player
disconnect(trackResultPlayer_);
disconnect(trackResultAnimator_);
}
if (videoPlayerWindow_)
{
videoPlayerWindow_->reset();
// Disconnect the video player window from source movie data
disconnect(videoPlayerWindow_);
}
// Disconnect this module from its children as they would be connected again
disconnect(this);
If I am to use smart pointers, instead of raw pointers, how could this problem be alleviated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让每个类实现一个析构函数,该析构函数执行该类所需的所有清理/取消初始化。
创建该类的一个实例,并将其包装在
boost::shared_ptr
中。然后将其副本传递给需要访问该实例的每个函数。
并且智能指针将确保一旦该对象不再被使用(当所有共享指针都被销毁时),它们指向的对象就被销毁。它的析构函数被运行,并且所有的清理被执行。
与 C++ 中的一贯做法一样,尽可能使用 RAII。
每当您有像
x.reset()
或disconnect(x)
这样的代码时,您应该做的第一件事就是问自己“这不属于析构函数吗?”此外,每当您使用 x->y() 时,您都应该问自己:
Make each of your classes implement a destructor which performs all the cleanup/deinitialization you need for that class.
Create an instance of the class, and wrap it in a
boost::shared_ptr
.Then pass copies of that to every function which needs access to the instance.
And the smart pointer will ensure that once the object is no longer used (when all the shared pointers have been destroyed), the object they point to is destroyed. its destructor is run, and all the cleanup is executed.
As always in C++, use RAII whenever possible.
Whenever you have code like
x.reset()
ordisconnect(x)
, the first thing you should do is ask yourself "doesn't this belong in a destructor?"Further, whenever you use
x->y()
you should ask yourself:智能指针主要是管理所指向内存的工具。它们并不是意味着可以让您摆脱 NULL 值检查的负担...
在您的示例代码中,我看不到智能指针降低代码复杂性的潜力,除非您移动像 videoPlayerWindow_ 这样的调用->reset()、
disconnect(videoPlayerWindow_)
等放入类的析构函数中,videoPlayerWindow
是其实例。Smart pointer are primarily an instrument to manage the memory being pointed to. They are not meant as something, which would free you from the burden of NULL value checking...
In you example code, I don't see much potential for smart pointers reducing the code complexity, unless you move calls like
videoPlayerWindow_->reset()
,disconnect(videoPlayerWindow_)
and such into the destructor of the class,videoPlayerWindow
is an instance of.对 NULL 的检查不是问题——而且智能指针无论如何也不会干扰这一点。
Checks against NULL aren't a problem - and smart pointers don't meddle with that anyway.