vendredi 24 février 2017

Keep C++ Modularity without Passing Functions Between Classes

I want to keep my code modular. At the moment I have my code set-up to pass functions from a child class to a parent class. However it does not compile at all. Now I want to get rid of the passing functions all together but keep the 'modular-ness'.

Class Parent {
    virtual double A (vector<double>) = 0;
    virtual double B (vector<double>) = 0;
    virtual double C (vector<double>) = 0;
    virtual double D (vector<double>) = 0;
    double E (double (Parent::*fxn(vector<double>)) {
        // doing tons of stuff
        (this->*fxn)(vec);
        Simplex((this->*fxn)(vec)); //third party library
    }
};

Class Child1: Parent {
    virtual double A (vector<double>) {
        Child2 r;
        // using variables only declared in Child1
    }
    virtual double B (vector<double>) {
        Child2 r;
        // using variables only declared in Child1
    }
    void F () {
        E(&Parent::A);
        E(&Parent::B);
    }
};

Class Child2: Parent {
    virtual double C (vector<double>) {
        // using variables only declared in Child2
    }
    virtual double D (vector<double>) {
        // using variables only declared in Child2
    }
    void G () {
        E(&Parent::C);
        E(&Parent::D);
    }
};

Things I have tried to make passing functions work:

Virtual Function 1, Virtual Function 2, Virtual Function 3: "cannot declare variable ‘r’ to be of abstract type ‘Child2’"

Pointer Functions 1, Pointer Functions 2: "cannot convert ‘double (Child1::)(std::vector)’ to ‘Parent::fxn {aka double ()(std::vector)}’ in initialization" (The asterisks are making things italics.)

So, I want to re-organize my code to get around passing functions. But I have no idea how to do this without getting rid of 'function E' and repeating the code in functions A-D (aka destroying the modular-ness). Any tips/advice?

Aucun commentaire:

Enregistrer un commentaire