Could this depth system for a game be improved? I am still new to C++ and don't have a great insight on my coding yet, so I would be very grateful to anyone and everyone that gives advice.

Also, this code is meant to: keep all of my objects in an ordered fashion based on depth. I have a couple functions that allow for easy management and I made object friends with the depth manager because I only want the depthManager to have control over each objects idDepth and depth which are two different things. 

The reason I need a depth system is because I need to have objects execute there code in an orderly fashion, and I also need to have control of what objects are drawn first to last.

This class has been tested and works as expected.


Stay up to date with this project on GitHub.com





Abstract object class:

class object{
    // Placement Data
        unsigned int depth, idDepth, idObject, idMain;

    // Friends
        friend class depthManager;
        friend class objectManager;
protected:
    unsigned int getDepthId(){ return idDepth; }
public:
    virtual void update() = 0;
    virtual void draw() = 0;
    unsigned int getDepth() { return this-&gt;depth; }
};


note: I have each object handle its own update and draw events for easier design. I'm mainly asking on approval for the depth system.




depthManager class

class depthManager{
private:
    std::map&lt;unsigned, std::vector&lt;object*&gt;* &gt; objectMap;

    void changeListPlacement( unsigned int depth, unsigned int position, int change);
public:
    void objectAdd( unsigned int depth, object* obj);
    void objectRemove( object* obj );
    void objectMove( unsigned int depth, object* obj );
};



depthManagers Functions:

void depthManager::objectAdd(unsigned int depth, object *obj)
{
    obj-&gt;depth = depth;
        // Check if depth key existant
        if ( objectMap.find( depth ) != objectMap.end() )
        {
            std::vector&lt; object* &gt;* &amp;refVec = objectMap[ depth ];
            refVec-&gt;push_back( obj );
            obj-&gt;idDepth = (unsigned)(int)refVec-&gt;size() - 1;
        }
        else // Add new Key
        {
            objectMap[ depth ] = new std::vector&lt; object* &gt;;
            std::vector&lt; object* &gt;* &amp;refVec = objectMap[ depth ];
            refVec-&gt;push_back( obj );
            obj-&gt;idDepth = (unsigned)(int)refVec-&gt;size() - 1;
        }
}

void depthManager::changeListPlacement(unsigned int depth, unsigned int position, int change = -1)
{
    if ( objectMap.find( depth ) == objectMap.end() )
    {
        return;
    }

    std::vector&lt;object*&gt;* &amp;refVec =  objectMap[ depth ];
    for( unsigned int i = refVec-&gt;size() - 1; i &gt; position; i -- )
    {
        object* &amp;pObj =  refVec-&gt;at( i );
        pObj-&gt;idDepth += change;
    }
}

void depthManager::objectRemove(object *obj)
{
    if (  objectMap.find( obj-&gt;depth ) == objectMap.end() )
    {
        std::cout &lt;&lt; "ERROR DEPTH NOT FOUND \n" &lt;&lt; std::flush ;
        return;
    }
    std::vector&lt;object*&gt;* &amp;refVec =  objectMap[ obj-&gt;depth ];
    changeListPlacement( obj-&gt;depth, obj-&gt;idDepth);
    refVec-&gt;erase( refVec-&gt;begin() + obj-&gt;idDepth );
}

void depthManager::objectMove(unsigned int depth, object *obj)
{
    this-&gt;objectRemove( obj );
    this-&gt;objectAdd( depth, obj );
}


previous verions

Depth Manager Source Code 1
Some remarks to your current map-based implementation:


The idiomatic way of accessing an item in a map and adding it if not present usually goes like this:

if not map.contains(key)
    map[key] = new value

value = map[key]
... modify value


So objectAdd could be shortened:

obj-&gt;depth = depth;
// make sure we have an object vector for the given depth
if ( objectMap.find( depth ) == objectMap.end() )
{
    objectMap[ depth ] = new std::vector&lt; object* &gt;;
}

std::vector&lt; object* &gt;* &amp;refVec = objectMap[ depth ];
refVec-&gt;push_back( obj );
obj-&gt;idDepth = (unsigned)(int)refVec-&gt;size() - 1;

In objectRemove it is apparently illegal to pass an object with a non-existent depth. Printing an error message to stdout is not the best way to handle an error like that. You should throw an appropriate exception (fail early is a valuable debugging tool) or allow it by ignoring invalid depths.
It is not imminently clear what objectMove exactly does based on its name and the names of its parameters. It looks like it moves an object to a different depth. So a better name and signature might be:

void depthManager::changeDepth(unsigned int newDepth, object *obj)

In class object the methods getIdDepth() and getDepth do not modify the object state so you should consider making them const (i.e. unsigned int getDepth() const { return this-&gt;depth; }).
I'm not 100% convinced of the idDepth property. It basically just reflects the current position of the object in the depth-list and you are writing a fair amount of boiler plate code to keep it that way. This increases the complexity of the class somewhat. I'd revisit the concept and check if I can't get by without it.