C++ Design Pattern for Passing a Large Number of Parameters

前端 未结 6 932
小鲜肉
小鲜肉 2020-12-25 13:44

I have a reasonably-sized class that implements several logically-related algorithms (from graph theory). About 10-15 parameters are required as input to the algorithm. Thes

6条回答
  •  盖世英雄少女心
    2020-12-25 14:19

    suggestion Why don't you do this instead:

    class Algorithm {
    public:
    Algorithm::Algorithm(AlgorithmGlobals const & globals) : globals_(globals) {}
    
        void runAlgorithm(); // use globals_ inside this function
    
       private:
        const AlgorithmGlobals globals_;
        };
    

    Now you can use it as such:

    AlgorithmGlobals myglobals;
    myglobals.somevar = 12;
    
    Algorithm algo(myglobals);
    

提交回复
热议问题