In reverse order:
yes, it's expected
private = accessible by only that class
protected = accessible by that class and those derived from it
public = accessible by everything
see the table in the "Inheritance between classes" section on this page:
Friendship and inheritance
http://www.cplusplus.com/doc/tutorial/inheritance/
1- how to make this constructor work? |
I'm not sure it's going to work without some adjustments to CharHandler.
Your CharHandler class currently has a Player member. If you add a texture parameter to the Character constructor, which Player is derived from, then you'll need to have the texture available when you construct the Player.
But I can see CharHandler loading a texture in its constructor body, which is run after its members (including the player) are constructed.
You will have to either (a) set the texture after construction or (b) change the Player member to a Player* (or std::unique_ptr<>) member and construct it dynamically after loading the required texture.
And you might consider passing the name to the Character constructor.
Andy
Using std::unique_ptr and initializer list
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
|
class Character{
private:
std::string name; //character's name
sf::Vector2f pos; //character's position
sf::Sprite char_sprite; //character's sprite
public: // note it's const sf::Texture& not sf::Texture
Character(const std::string& charname, const sf::Texture& texture);
~Character();
};
// using Sprite (const Texture &texture) form of sprite constructor
Character::Character(const std::string& charname, const sf::Texture& texture)
: name(charname), char_sprite(texture){
}
// Player constructor now passing parameters onto Character constructor
class Player : public Character{
public:
Player(const std::string& charname, const sf::Texture& texture)
: Character(charname, texture) {
}
~Player();
};
class CharHandler{
std::vector <sf::Texture> char_textures; //vector for all character's textures
std::unique_ptr<Player> player;
public:
CharHandler() {
char_textures.resize(1);
char_textures[0].loadFromFile("images/test.png");
std::unique_ptr<Player> newPlayer(new Player("Stauricus", char_textures[0]));
player = std::move(newPlayer);
}
~CharHandler();
};
|