friend declaration of interface class in abstract base class

985 views Asked by At

So I was thinking about trying to program the tetris game in c++ with the sfml and I thought about how I could implement the game and realized it could be a could exercice on inheritance. I thought I will have:

-an abstract base class that will represent a tetromino (shapes puzzle pieces in tetris) : its called Tetromino_Base

-classes that'll inherit from the base class to represent a specific tetromino, since there are different shapes like squares, bars... etc.

-an interface class called Tetromino which is the type the user will manipulate.

As I go I realized that my class interface needs access to the abstract class members, hence I made a friend declaration for it but I thought: is it good practice to do so? Make a friend declaration your interface class in the base class?

Here's the main part of the code:

class Tetromino;

class Tetromino_Base {
    friend class Tetromino;
public:
    virtual ~Tetromino_Base() {};
protected:
    float m_x, m_y;
    sf::Texture m_texture;
    sf::VertexArray m_vertices;

    virtual bool create(const sf::Vector2f&, const float&, //position of the object, scale of the object
    const std::string&, const sf::Vector2f&, const float&) = 0; 
    //file name for the texture, position in the texture, scale of the texture
};

class Square : public Tetromino_Base {
public:
    ~Square() {}
private:
    bool create(const sf::Vector2f&, const float&, 
    const std::string&, const sf::Vector2f&, const float&);
};

class Tetromino : public sf::Drawable {
public:
    Tetromino(int tetroCode = 0);

    bool create(const sf::Vector2f& sorigins, const float& ssc, 
    const std::string& fileName, const sf::Vector2f& torigins, const float& tsc){
        //access needed here
        return p->create(sorigins,ssc,fileName,torigins,tsc);
    }

private:
    Tetromino(Tetromino_Base* ptr) : p (ptr) {}
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        //access needed here
        states.texture = &(p->m_texture);
        target.draw(p->m_vertices, states); 
    }
    Ptr<Tetromino_Base> p;
};
0

There are 0 answers