Creating an optimized, fully functional TextureManager in SDL After I've discussed pointer semantics with Loki Astari previously, I finally managed to code the TextureManager class using auto_pointers. But the problem is that I need to pass an SDL_Renderer object to my class' constructor as an auto_ptr. But the object I want to pass is a raw pointer.

How should I do it? Also, I'm not so sure about the aout_pointers I've written. Can you help me, please?

TextureManager.h

#pragma once //using visual C++

#include &lt;unordered_map&gt;
#include "Texture.h"
#include &lt;memory&gt;

/**
    This class helps the user to manage large 
    amount of texture at the same time (disposing them, setting 
    their color, their renderer, ...). The dispose method of this class 
    should be called before the end of the program (there will be no issues
    if it is not called : the destrctor of the TextureManager class will call
    the destructor of the @Texture class ! But it is better to call it, since
    it will release all ressources when you want !).This class contains a dynamic
    array, so the user can add @Texture pointers threw out the programs
    evolution.
*/

typedef std::unordered_map&lt;int, Texture*&gt; atlasType;//cannot use auto_ptr in STL container !

class TextureManager
{
public:
    TextureManager(std::auto_ptr&lt;SDL_Renderer&gt; pRenderer);
    ~TextureManager();

    void setRenderer(std::auto_ptr&lt;SDL_Renderer&gt; pRenderer);
    bool LoadFromFile(unsigned int ID, const char* fileName);

    bool unloadTexture(unsigned int ID);
    void dispose();

    Texture&amp; getTexture(unsigned int ID);
    Texture getTextureCopy(unsigned int ID);

private:
    std::auto_ptr&lt;SDL_Renderer&gt; m_pRenderer;
    atlasType m_textureAtlas;
};


TextureManager.cpp

#include "TextureManager.h"


TextureManager::TextureManager(std::auto_ptr&lt;SDL_Renderer&gt; pRenderer)
    : m_pRenderer(pRenderer)
{}

TextureManager::~TextureManager(){
    dispose();
}

void TextureManager::dispose(){
    for (atlasType::iterator it = m_textureAtlas.begin(); it != m_textureAtlas.end(); ++it){
        delete (it-&gt;second);
    }
    m_textureAtlas.clear();
}


bool TextureManager::unloadTexture(unsigned int ID){
    Texture *pTempTexture = m_textureAtlas[ID];

    if (!pTempTexture)
        return false;

    delete pTempTexture;//dispose texture
    pTempTexture = nullptr;
    m_textureAtlas.erase(ID);

    return true;
}

Texture&amp; TextureManager::getTexture(unsigned int ID){
    return *(m_textureAtlas[ID]);
}

Texture TextureManager::getTextureCopy(unsigned int ID){
    return *(m_textureAtlas[ID]);
}

bool TextureManager::LoadFromFile(unsigned int ID, const char* fileName){
    Texture *pTexture = m_textureAtlas[ID];

    if (pTexture == nullptr){
        pTexture = new Texture();
        pTexture-&gt;setRenderer(m_pRenderer.get());//Texture uses raw pointer;

        m_textureAtlas[ID] = pTexture;//will be deleted in dispose;
    }
    if (!pTexture-&gt;LoadFromFile(fileName))
        return false;

    return true;
}

void TextureManager::setRenderer(std::auto_ptr&lt;SDL_Renderer&gt; pRenderer){
    m_pRenderer = pRenderer;
}


Here's where I can not call the constructor (raw pointer &lt;-> auto_ptr):

/* ......Declarations........... */

SDL_Renderer * m_pRenderer = nullptr;

/* etc..........*/

m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);


/*Trying to create TextureManager*/


m_textureManager = TextureManager(m_pRenderer);//fail to compile : is not of type auto_ptr&lt;SDL_Renderer&gt; ! what shall I do ?
You don't need a texture copy.

Texture&amp; getTexture(unsigned int ID);
// Texture getTextureCopy(unsigned int ID);


This works:

Texture&amp;  t1 = tm.getTexture(1);  // Gets a reference.
Texture   t2 = tm.getTexture(2);  // Gets a reference but because t2
                                  // is an object copies it from the
                                  // original into `t2`


auto_ptr is deprecated

std::auto_ptr&lt;SDL_Renderer&gt;


Prefer the modern version std::unique_ptr it understands move semantics.

std::unique_ptr&lt;SDL_Renderer, TypeThatKnowsHowToDeleteSDLRendor&gt;  renderer;


Also std::unique_ptr and std::auto_ptr just call delete on the pointer. I am betting that the SDL library has a special function to delete render objects. In which case you need to add a class that knows what function to call.

Also std::unqiue_ptr can be used in containers:

typedef std::unordered_map&lt;int, Texture*&gt; atlasType;//cannot use auto_ptr in STL container !


But you don't even need to be using pointers. Texture is not polymorphic. Just create the object in the container (not the pointer).

std::unordered_map&lt;int, Texture&gt;  AtlasType;


Naming conventions.

It is usall to give User defined types an initial capitol letter. It is usual to give methods and member variables an initial lower case letter.

std::unordered_map&lt;int, Texture&gt;  AtlasType;
             //                   ^  Its a user defined type.
AtlasType                         textureAtlas;
             //                   ^ its an addressable object.


Prefer the new range based for

for (atlasType::iterator it = m_textureAtlas.begin(); it != m_textureAtlas.end(); ++it){
    delete (it-&gt;second);
}


As mentioned last time. Prefer auto, prefer std::begin/std::end over method calls. and prefer range based for over all of them:

//
for (auto it = std::begin(m_textureAtlas); it != std::end(m_textureAtlas); ++it){
    delete (it-&gt;second);
}

//
for(auto item: m_textureAtlas) {
    delete item.second;
}

//
std::for_each(std::begin(m_textureAtlas), std::end(m_textureAtlas),
               [](AtlasType::value_type&amp; v){delete v.second;});


Compilation Failure:

 m_textureManager = TextureManager(m_pRenderer);
                               //  ^^^^^^^^^^^ that is not an auto_ptr

 // You have to create the auto_ptr object.
 std::auto_ptr&lt;SDL_Renderer&gt;   rend(m_pRenderer)
 m_textureManager = TextureManager(rend);


Note. When you give up ownership of the object you should not be using it from the original pointer again. The only way to access the render object should not be through the TextureManager that owns it. If you have kept another pointer to the renderer that you are cheating ownership semantics.

Writting a shard render object

class CRendWrap
{
    public:
        CRendWrap(SDL_Window* w, int x, int y)
            : SDL_CreateRenderer(w, x, y)
        {}
        ~CRendWrap()
        {
            if (rend != nullptr) {
                SDL_DestroyRenderer(rend);
            }
        }
        // Disable Copy
        CRendWrap(CRendWrap const&amp;)             = delete;
        CRendWrap&amp; operator=(CRendWrap const&amp;)  = delete;
        // Allow Move
        CRendWrap(CRendWrap&amp;&amp; move)
            : rend(nullptr)
        {
            using std::swap;
            swap(rend, move.rend);
        }
        CRendWrap&amp; operator=(CRendWrap&amp;&amp; move)
        {
            using std::swap;
            swap(rend, move.rend);
        }
    prviate:
        SDL_Renderer*   rend;
};

typedef std::shared_ptr&lt;CRendWrap&gt;   CRender;

// Usage.
CRender  renderer = std::make_shared&lt;CRendWrap&gt;(win, 10, 20);