c++ - Passing a collection of partially editable objects to an algorithm -


i simplified problem simple example : immagine manage collection of elements std::vector<element>, each element having several members :

struct element {   public:     double foo;     double bar; }; 

then, want define abstract class barevaluator, algorithms computing values of b values of a. first idea following :

class barevaluator {   public:     virtual void evaluate(std::vector<element>& elements) const = 0; }; 

from that, can implement several algorithms, example, algorithme computing bar values square of foo values :

class sqrbarevaluator {   public:     virtual void evaluate(std::vector<element>& elements) const     {       for(unsigned long int = 0; < elements.size(); ++i)         elements[i].bar = elements[i].foo * elements[i].foo;     } }; 

this working well. think it's not architecture, because my algorithm able modify foo values. don't want that.

then able give collection algorithm kind of "filter" allowing modify bar variable , not foo variable in each element. possible c++98 ? have no idea how that.

remark 1 : don't want public or private in element. can immagine want create algorithms fooevaluator computing foo values bar values, writing access foo , not bar.

remark 2 : algorithm can require collection compute each value.

maybe should pull loop out of interface.

class barevaluator {   public:     virtual double evaluate(const element& element) const = 0; };  class sqrbarevaluator {   public:     virtual double evaluate(const element& element) const     {       return element.foo * element.foo;     } }; 

then call this:

std::vector<element> elements; ... (std::vector<element>::iterator = elements.begin(); != elements.end(); ++it) {     it->bar = barevaluator.evaluate(*it); } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -