mercredi 10 janvier 2018

Design an interface with appropriate declarations of Base class pointers - cpp

I have a little design problem.

Assume I have this interface:

class IBase {
public:
    virtual void Run() = 0;
    virtual void SetData(IData* data) = 0;
    virtual ~IBase() = 0;
};

I want to implement derived classes, so I need to build a derived class for IData (let's call it DData). The problem is that in SetData method that I implement - I need to refer to specific members in DData, but I can't because the pointer is of IData (SetData method not only assign "m_data = data")

class DData : public IData {
private:
    int m_i;
};

What is the best way to overcome this problem?

dynamic cast on SetData? Changing the interface (in this way?)? Other option?

Aucun commentaire:

Enregistrer un commentaire