我有一个shared_ptr
boost::variant< CClassPtr1, CClassPtr2, etc... >
由2个班级共享。
这是个坏主意吗?
我注意到boost::get<T>()
和boost::apply_visitor<T>
默认不将指针类型作为参数(即我必须取消引用指针)?
哦,我忘了提及CClassPtr
类型本身也是共享指针。
// This is the variant pointer type
shared_ptr< boost::variant< CTexture1DPtr, CTexture2DPtr > > pResource;
// Note: this is a dummy constructor the real thing is assigned later on in the program
// with a actual texture1D instance.
pResource = std::make_shared<ShaderResource>( CTexture1DPtr() );
// psuh back the variant to a std::vector of shared_ptr< variant > types
if ( (*itor).second & CEffect::SOF_VERTEX )
{
pass->m_pVertexShader->AddShaderResource(pResource);
}
// Create a variables accessor
CGlobalVariablePtr pGlobalVar = Engine::CreateGlobalVariable( pVar->GetShaderDimensions() );
// this owns a copy of the variant pointer
pGlobalVar->m_memberData.m_shaderResource = pResource;
请注意,如果在堆栈上执行了上述操作,那么{I} pass->m_pVertexShader->AddShaderResource(pResource);
将导致与分配给pGlobalVar的变量不同。