SFML community forums

Help => Graphics => Topic started by: Samyboy on May 25, 2010, 11:58:37 am

Title: Inherit from sf::String
Post by: Samyboy on May 25, 2010, 11:58:37 am
Hello,

Is it possible to inherit from sf::String? I am currently trying to write a Score class to make my life easier, but it doesn't quite work.

Here is how I inherit from sf::String, the constructor of sf::String gets called in my constructor, btw.

Code: [Select]
#ifndef SCORE_H_INCLUDED
#define SCORE_H_INCLUDED

#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

namespace readOnly {

class Score : public sf::String {
    public:
        Score(float PosX = 0, float PosY = 0);
        ~Score();

        void SetScore(int score);
        void Add(int addScore = 1);

        const int& GetScore() const;

        void Update();
    private:
        int m_score;

};

}

#endif // SCORE_H_INCLUDED
Title: Inherit from sf::String
Post by: Nexus on May 25, 2010, 12:41:02 pm
It is possible, but not meaningful in your situation. You should prefer containment/aggregation to inheritance where possible. How is your class related to sf::String?

By the way, you shouldn't implement the destructor unless the copy constructor and assignment operator are implemented, too, or hidden (Rule of the Big Three). If the destructor is empty, just don't declare it.
Title: Inherit from sf::String
Post by: Samyboy on May 25, 2010, 12:57:40 pm
Quote from: "Nexus"
It is possible, but not meaningful in your situation. You should prefer containment/aggregation to inheritance where possible. How is your class related to sf::String?

By the way, you shouldn't implement the destructor unless the copy constructor and assignment operator are implemented, too, or hidden (Rule of the Big Three). If the destructor is empty, just don't declare it.


Hello,

Thank you very much for your input!