Dri*_*ism 3 c++ boost smart-pointers
为了编写更好的代码,我已经致力于使用boost的智能指针.
应该boost::shared_ptr像这样初始化吗?
boost::shared_ptr<TypeX> px(new TypeX);
Run Code Online (Sandbox Code Playgroud)
我的混淆来自类似于此的代码块:
void MyClass::FillVector()
{
boost::shared_ptr<TypeX> tempX;
if(//Condition a)
{
boost::shared_ptr<TypeX> intermediateX(new TypeA);
tempX = intermediateX;
}
else
{
boost::shared_ptr<TypeX> intermediateX(new TypeB);
tempX = intermediateX;
}
_typeXVec.push_back(tempX); // member std::vector< boost::shared_ptr<TypeX> >
}
Run Code Online (Sandbox Code Playgroud)
是否有一种可接受的方法来跳过该中间版本的shared_ptr,同时将其保持在范围内以推回到_typeXVec?
谢谢.
编辑:想要澄清TypeA和TypeB都是TypeX的孩子.
boost::shared_ptr<X> p;
if( condition ) {
p.reset( new A() );
}else {
p.reset( new B() );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1504 次 |
| 最近记录: |