SFML community forums

Help => Graphics => Topic started by: vechestva on January 12, 2014, 06:35:49 am

Title: How to create a copy of the class?
Post by: vechestva on January 12, 2014, 06:35:49 am
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));
Title: Re: How to create a copy of the class?
Post by: Raincode on January 12, 2014, 11:12:58 am
Well, pardon me, but why would you want to do such a thing? Create a class exactely like the one SFML provides? Don't see any point in that... And don't use those naked news und deletes. Just write:
Rectangle line(sf::Vector2f(50, 10)); and let the constructors and destructors take care of everything...
Title: Re: How to create a copy of the class?
Post by: vechestva on January 12, 2014, 11:46:36 am
Well, pardon me, but why would you want to do such a thing?
To modify the base class.
Title: Re: How to create a copy of the class?
Post by: Nexus on January 12, 2014, 02:34:39 pm
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.