These lines of code are run:
Enemy* enemy = new Enemy(m_pSceneManager,"enemy"+ss.str());
std::cout << "Enemy name = " << std::string(enemy->name) << std::endl;
Add(enemy->name,enemy);
in EnemyManager.cpp. EnemyManager inherits from GameObjectManager:
void GameObjectManager::Add(sf::String name,VisibleGameObject* obj){
std::cout << "GameObjectManager obj name = " << std::string(obj->name) << std::endl;
gameObjects.insert(std::pair<sf::String,VisibleGameObject*>(name,obj));
}
Enemy inherits from VisibleGameObject, which both have a public sf::String ‘name’ variable but only Enemy initializes it at any point (in its constructor). The output in the console window is this:
Enemy name = enemy0
GameObjectManager obj name =
Why does the name revert to nothing? I’m assuming it’s something to do with the fact that the argument is a VisibleGameObject* and not an Enemy* – if it is, how can I work around this? If I’ve made a mistake somewhere else, please tell me if there are any details I am missing that are required.
Thanks
Since, as you say both have a public sf::String ‘name’ variable but only Enemy initializes it at any point (in its constructor). , there are two
namesubojects: one is a member ofEnemyand another is a subobject ofVisibleGameObject. Inside functionAddyou explicitly refer to the latter one, so that is what you get, and it is not initialised.You probably want to make
namea virtual function returning string; this will make functionAddto employ runtime polymorphism and callname()from derived class, i.e. fromEnemy.Simpler (but not necessarily better) design would be to make
nameprotected or public member ofVisibleGameObjectand letEnemyset it; that also means you will most likely want to removenamefromEnemy, otherwisenameinherited fromVisibleGameObjectwill be hidden.