【发布时间】:2009-09-22 10:21:40
【问题描述】:
很遗憾,我没有机会在实际开发中使用智能指针(主管认为它太“复杂”且浪费时间)。但是,我打算将它们用于我自己的东西......
我遇到过在模块完成后或在加载新数据时取消初始化模块的情况。当我使用指针时,我发现我的代码中充斥着诸如此类的 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);
如果我要使用智能指针而不是原始指针,如何缓解这个问题?
【问题讨论】:
-
记住 new 不会返回 NULL(除非你明确要求)。
标签: c++ pointers smart-pointers