I have a Player class that derives from Humanoid and Humanoid derives from Sprite like so
Sprite -> Humanoid -> Player
So far so good. Sprite has the virtual method
virtual sf::Sprite getSprite() { return sprite_; }
where sprite is an image object that can be drawn onto the screen.
Humanoid declares virtual sf::Sprite getSprite();
Player defines virtual sf::Sprite getSprite() { return sprite_; }
Sprite has a member sprite_; this is what I want to do:
1) Create a player class while passing a sprite like so
Player::Player(sf::Image image)
: Humanoid()
{
sprite_.SetImage(image); // Converts sf::Image to sf::Sprite
}
2) Drawing it on the screen from my Game class like so
sf::Image playerImage;
if(!playerImage.LoadFromFile("Link/Link.png"))
std::cin.get();
Player *player = new Player(playerImage);
/* ... */
App.Draw(player->GetSprite());
Testing the program, I have found that the player->getSprite() method is indeed called, but I fear that I may have hidden the Sprite::getSprite() method instead of actually deriving and using it. Since the Sprite class has a protected sprite_ member, I’m confused as to which sprite_ object I’m actually returning when I call the function.
Also, it seems like the image file got lost somewhere along the line, and before I post in the SFML forum I would like some confirmation as to whether or not my inheritance tree is actually correct or not.
If the
Spriteclass has asprite_member and none of the derived classes (Humanoid/Player) have added a member with the same name then there is only onesprite_object for each instance and the function should work as expected. I don’t understand why you’re overridinggetSpriteif you give it the exact same behaviour though.