What is the syntax to declare a unique_Ptr variable in a header, then assign it later in the constructor?
c++-cli, unique-ptr
Solution
The following should work fine:
spriteBatch = std::unique_ptr<SpriteBatch>(new SpriteBatch(m_d3dContext.Get()));
Alternatively, you can avoid repeating the type name with some `make_unique` function.
spriteBatch = make_unique<SpriteBatch>(m_d3dContext.Get());
There's also the `reset` member:
spriteBatch.reset(new SpriteBatch(m_d3dContext.Get()));
But, since you mention a constructor, why not just use the member initialization list?
CubeRenderer::CubeRenderer()
: spriteBatch(new SpriteBatch(m_d3dContext.Get())) {}
Problem
I have coded the following, and am very new to c++, and it feels clumsy. I am trying to give 'spriteBatch' (a unique_Ptr) class scope. Here's the header file: ``` ref class CubeRenderer : public Direct3DBase { public: CubeRenderer(); ~CubeRenderer(); private: std::unique_ptr<SpriteBatch> spriteBatch; }; ``` Then in the cpp file Constructor, this: ``` std::unique_ptr<SpriteBatch> sb(new SpriteBatch(m_d3dContext.Get())); spriteBatch = std::move(sb); ``` It just seems clumsy the way I had to create 'sb' and move it to 'spriteBatch'. attempting to assign directly to 'spriteBatch' failed (maybe I simply don't know the proper syntax). Is there a way to avoid needing to use 'sb' & std::move? Thank you.