Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7093797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:29:03+00:00 2026-05-28T08:29:03+00:00

These lines of code are run: Enemy* enemy = new Enemy(m_pSceneManager,enemy+ss.str()); std::cout << Enemy

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:29:04+00:00Added an answer on May 28, 2026 at 8:29 am

    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 name subojects: one is a member of Enemy and another is a subobject of VisibleGameObject. Inside function Add you explicitly refer to the latter one, so that is what you get, and it is not initialised.

    You probably want to make name a virtual function returning string; this will make function Add to employ runtime polymorphism and call name() from derived class, i.e. from Enemy.

    Simpler (but not necessarily better) design would be to make name protected or public member of VisibleGameObject and let Enemy set it; that also means you will most likely want to remove name from Enemy, otherwise name inherited from VisibleGameObject will be hidden.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run these 2 lines of code I get what I expect: Func<int,
In C#, what is the difference (if any) between these two lines of code?
I grabbed this code form JCarousel and just trying to understand these lines below.
The code below tries to parse a file containing these 3 lines: 0 2
I`m a new guy to c# and I try to add a some code
Is there a way of calling a method/lines of code multiple times not using
Is there a way to find the number of lines of code used in
I want to make a pause between two lines of code, Let me explain
Relevent code: $status = $db->run( INSERT INTO user_wall (accountID, fromID, text, datetime) VALUES (:toID,
ANSWER: If you ever see these lines and are mistified like I was, here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.