I am trying to use smart pointers to store COM objects in my class, avoiding ComPtr. Can unique_ptr be used for this purpose?
I am completely new to smart pointers and still am a little confused. Please consider the following simplified code:
class Texture
{
private:
struct ComDeleter
{
operator() (IUnknown* p)
{
p.Release();
delete p;
}
}
ID3D11Texture* m_dumbTexture;
std::unique_ptr<ID3D11Texture, ComDeleter> m_smartTexture;
public:
ID3D11Texture* getDumbTexture() const { return m_dumbTexture; }
ID3D11Texture* getSmartTexture() const { return m_smartTexture.get(); }
}
Texture::Texture() :
dumbTexture (NULL),
smartTexture (nullptr)
{
}
Texture::init()
{
D3DX11CreateTexture(&m_dumbTexture);
D3DX11CreateTexture(&m_smartTexture.get());
}
So my problems are: what should getter return (raw pointer or unique_ptr instance) and how can I pass unique_ptr to the function that creates the resource?
source
share