mardi 6 novembre 2018

How to avoid polymorphism?

I have a data type OperationSequence. Below is OperationSequence.h

class OperationSequence
{
public:
    void appendOperation(std::function<double(double)> operation);
    void moveOperation(int operation_index);
    void eraseOperation(int operation_index);

    const std::vector<std::function<double(double)>>& data() const;
private:
    std::vector<std::function<double(double)>> operation_sequence;
};

double executeSequence(const OperationSequence& operation_sequence, double value);
void executeSequence(const OperationSequence& operation_sequence, const std::string& file_name);

I have to implement printOperationSequence(const OperationSequence& operation_sequence).

Assignment set a requirement on operation to be f: double -> double. Some operations like Addition and Multiplication were also requested. Obvious implementation would be to create an Interface Operation and have it be callable with f: double -> double and have a std::string getName() method.

What would be a good way for OperationSequence to remain this generic but also making it easy and efficient to print out OperationSequence in a meaningful way?

Meaningful way being something like Multiplication, Addition, ...

Is delegating construction to some other class that will also create a operation_name_sequence a good idea?

P.S. Feel free to improve the question title :D

Aucun commentaire:

Enregistrer un commentaire