S
suresh
Hi,
Kindly look at the code below of a function object and its application
in for_each.
class DistBwObjFunObj
{
public:
DistBwObjFunObj(Object o);
void operator()(Point p); //finds the distance and accumulates them
double totalDist(); //returns the accumulated dist
private:
Object obj;
double dist;
};
DistBwObjFunObj:istBwObjFunObj(Object o)bj(o),dist(0){}
void DistBwObjFunObj:perator()(Point p){
dist += distPoint(p,obj.closestPoint(p));
}
double DistBwObjFunObj::totalDist(){
return dist;
}
//Application code segment:
DistBwObjFunObj mydist(o);
for_each(f.begin(),f.end(),mydist);
double d = mydist.totalDist();
My problem: the value of d is always equal to the value of dist at the
time of construction, which is zero now. Why is it so?
I feel that, the constructor is called everytime inside the for_each.
Why is it so?
Thanks for your comments
suresh
Kindly look at the code below of a function object and its application
in for_each.
class DistBwObjFunObj
{
public:
DistBwObjFunObj(Object o);
void operator()(Point p); //finds the distance and accumulates them
double totalDist(); //returns the accumulated dist
private:
Object obj;
double dist;
};
DistBwObjFunObj:istBwObjFunObj(Object o)bj(o),dist(0){}
void DistBwObjFunObj:perator()(Point p){
dist += distPoint(p,obj.closestPoint(p));
}
double DistBwObjFunObj::totalDist(){
return dist;
}
//Application code segment:
DistBwObjFunObj mydist(o);
for_each(f.begin(),f.end(),mydist);
double d = mydist.totalDist();
My problem: the value of d is always equal to the value of dist at the
time of construction, which is zero now. Why is it so?
I feel that, the constructor is called everytime inside the for_each.
Why is it so?
Thanks for your comments
suresh