c++ - Extending object scope out of if statement -
i have following c++ design problem.
suppose have following class:
class model { model(int numberofmodels, int flag=-1) : _models(numberofmodels), _flag(flag){ } void buildmodel(){ (int id=0; id<_models.size(); ++id) { if (flag == -1){ builderone builder; builder.build(&_models[id]); } else { buildertwo builder; builder.build(&_models[id]); } } } private: vector<simplemodel> _models; int _flag; };
where member function "buildmodel" builds vector of "simplemodel" object. "builderone" , "buildertwo" different builder class implements "build" method (or can inherited same basebuilder class using crtp).
but above implementation quite cumbersome, since type of builder should predetermined "_flag" before entering loop. want following implementation of "buildmodel" method:
void buildmodel(){ if (flag == -1){ builderone builder; else buildertwo builder; (int id=0; id<_models.size(); ++id) builder.build(&_models[id]); }
however, above code doesn't compile because 1) object "builder" not visible after if statement 2) type of "builder" cannot decided @ compile time.
the above functionality can realized making "build" method virtual function in basebuilder. virtually function not considered solution because of various reasons in our library. inheritance (like crtp) acceptable.
some 1 around problem?
with [insane] requirement virtual functions cannot used current implementation broken. every time new builder type added have update model
class. may want (your post unclear on this) i'll try cover both approaches single solution.
first can start taking advantage of static polymorphism , place main functionality in function template. around lack of virtual functions in reducing amount of code necessary use it.
class model { public: template<class buildertype> buildertype buildmodel() { buildertype builder; // perform other tasks here (int id = 0; id<_models.size(); ++id) { builder.build(&_models[id]); } // perform other tasks here return builder; } };
this allow use object type long implements build
function takes pointer instance of simplemodel
. have option of taking builder argument function allow automatic type deduction. whether make public, private, or protected , how decide move forward. may need if want limit buildmodel
function specific set of builders can make protected or private , provide public function simplifies building process.
class model { public: void buildmodel() { switch (flag_) { case -1: buildmodelbytype<builderone>(b); break; default: buildmodelbytype<buildertwo>(b); break; } } protected: template<class buildertype> buildertype buildmodelbytype() { buildertype builder; // perform other tasks here (int id = 0; id<_models.size(); ++id) { builder.build(&_models[id]); } // perform other tasks here return builder; } };
whether take builders argument or return result of building process you. post wasn't specific aspect of problem should adaptable whatever you're towards.
Comments
Post a Comment