The issue is likely that when you set the texture in the initialization list, the texture has no size and thus the texture rect of the sprite will also be set to no size. Then when you load the texture, you never adjust the texture rect and thus nothing is shown.
The sprite.setTexture call isn't necessary, as you've already set the texture by passing it to the sprite, but what you want to do is call sprite.setTextureRect(IntRect({0, 0}, Vector2i(texture.getSize()))) to match the texture size.
MyDrawable::MyDrawable() : sprite(texture) {
if (!texture.loadFromFile("image.png")) {
std::cerr << "Error loading texture!" << std::endl;
} else {
sprite.setTextureRect(sf::IntRect({0, 0}, sf::Vector2i(texture.getSize())));
}
}
Or if you're fine with handling exceptions, you could also do:
MyDrawable::MyDrawable() : texture("image.png"), sprite(texture) {
}
sprite.setTextureRect(IntRect({0, 0}, Vector2i(texture.getSize())));
You can also reset it automatically using:
sprite.setTexture(texture, true); // note the added "true" here
but internally it just does the code that eXpl0it3r posted anyway and doing that manually gives more control in case you might want to use a part of the texture instead of the entire image.
In fact, if you use eXpl0it3r's code, you'll likely need to add "sf::" to the IntRect and the Vector2i ;)
On a separate note, there is no longer any need to use "virtual" keyword when you provide an "override" keyword.