In an effort to write better code, I've committed to using boost's smart pointers.
Should a boost::shared_ptr
always be initialized like so?
boost::shared_ptr<TypeX> px(new TypeX);
My confusion has risen from a code block similar to this:
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> >
}
Is there an accepted way to skip that intermediate shared_ptr while keeping it in scope to push back to _typeXVec?
Thank you.
Edit: Wanted to clarify that TypeA and TypeB are both children of TypeX.
boost::shared_ptr<TypeX> tempX((condition ? new TypeA : New TypeB));