Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to create a copy of the class?  (Read 2320 times)

0 Members and 2 Guests are viewing this topic.

vechestva

  • Newbie
  • *
  • Posts: 25
    • View Profile
How to create a copy of the class?
« 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));
I do not know much English.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: How to create a copy of the class?
« Reply #1 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...

vechestva

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: How to create a copy of the class?
« Reply #2 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.
I do not know much English.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to create a copy of the class?
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: