1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <map>
#include <vector>
struct Object
{
Object( const std::string& path, int x, int y ) : path(path), x(x), y(y) {}
void Init( /* ... */ ) { /* ... */ }
// ...
private:
std::string path ;
int x ;
int y ;
};
struct Engine
{
// ...
void CreateObject( const std::string& name, const std::string& path, int x, int y )
{
ObjectName[name].emplace_back( path, x, y ) ; // C++11
// or: ObjectName[name].push_back( Object( path, x, y ) ) ; // C++98
ObjectName[name].back().Init( /* ... */ ) ;
// or alternatively:
// std::vector<Object>& vec = ObjectName[name] ;
// vec[ vec.size() - 1 ].Init( /* ... */ ) ;
}
private: std::map< std::string, std::vector<Object> > ObjectName ;
// ...
};
int main()
{
Engine engine ;
engine.CreateObject( "bullet", "abc/def/bullet.png", 100, 70 ) ;
engine.CreateObject( "missile", "abc/def/missile.png", 50, 120 ) ;
engine.CreateObject( "bullet", "abc/def/bullet.png", 0, 45 ) ;
engine.CreateObject( "missile", "abc/def/missile.png", 96, 128 ) ;
// etc.
}
|