Greetings!
I want to make a base class Depictable for graphics that will hold my methods.
It inherits from sf :: Drawable.
class Depictable: public sf::Drawable
{
...
public:
virtual ~Depictable() {}
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const {}
...
};
I want to do is an exact copy of the class sf :: RectangleShape.
class Rectangle : public Depictable, public sf::RectangleShape
{
public:
virtual ~Rectangle() {}
explicit Rectangle(const sf::Vector2f& size = sf::Vector2f(0, 0)) { setSize(size); }
};
and then draw it.
Rectangle* line = new Rectangle(sf::Vector2f(50, 10));
line->setFillColor(sf::Color::White);
line->setPosition(100, 100);
app.draw(*line);
Naturally, the line is drawn.
How to make a class so that it is drawn?
except sf::RectangleShape* line = new Rectangle(sf::Vector2f(50, 10));
class Depictable: public sf::Drawable {...};
class Rectangle : public Depictable, public sf::RectangleShape {...};
That's a bad idea, you create the diamond of death.
I think there is a misunderstanding of inheritance: It's not the only way to add functionality in C++. It comes at a high cost, and alternatives (composition, free functions, static polymorphism) are often better.
Why does a Rectangle need to inherit your class? Why not just something like that?
class Rectangle
{
sf::RectangleShape shape;
};
Please tell us what you actually want to achieve, not how you think you should do it.