return a Type, or how to preserve a type of an object pointer?

前端 未结 3 1820
不知归路
不知归路 2021-01-20 13:34

I have a very complicated code structure, but the important bits are:

typical setup: I have a base class and two classes that derive from this base class and each ha

3条回答
  •  有刺的猬
    2021-01-20 14:34

    One way to achieve this is to add an interface method into the base class:

    class BaseSolver{
    virtual void SolverMethodToCallFromMain() = 0;
    ...
    };
    
    class SolverA : BaseSolver{
    public:
        std::string a;
        SolverA(TypeA objectA);
        virtual void SolverMethodToCallFromMain() {/*SolverA stuff here*/};
    };
    
    class SolverB : BaseSolver{
    public:
        int b;
        SolverB(TypeB objectB);
        virtual void SolverMethodToCallFromMain() {/*SolverB stuff here*/};
    };
    

    And in main:

    int main(){
        IOService ioService;
        BaseSolver* mySolver = ioService.getSolver();
        mySolver->SolverMethodToCallFromMain();
    }
    

提交回复
热议问题