why constructor is called in for_each funobj everytime?

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::DistBwObjFunObj(Object o):eek:bj(o),dist(0){}

void DistBwObjFunObj::eek: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
 
I

Ian Collins

Hi,
Kindly look at the code below of a function object and its application
in for_each.
class DistBwObjFunObj

What on Earth prompted you to use such a bizarre name?
{
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::DistBwObjFunObj(Object o):eek:bj(o),dist(0){}

void DistBwObjFunObj::eek: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?

Have you looked? Does your distPoint function return non-zero values?
 
Ö

Öö Tiib

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::DistBwObjFunObj(Object o):eek:bj(o),dist(0){}

void DistBwObjFunObj::eek: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

Maybe this will help:

mydist = std::for_each(f.begin(),f.end(),mydist);
 
K

Kai-Uwe Bux

suresh said:
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::DistBwObjFunObj(Object o):eek:bj(o),dist(0){}

void DistBwObjFunObj::eek: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?

Because, you don't change it.
I feel that, the constructor is called everytime inside the for_each.
Why is it so?

Not everytime. However, for_each() takes the functor argument by value.
Thus, a copy is created. Luckily, it returns the copy that is used
internally. Thus:

mydist = for_each( f.begin(), f.end(), mydist );

should do the trick (provided, the type has a correctly working assignment
operator).


Best

Kai-Uwe Bux
 
S

suresh

What on Earth prompted you to use such a bizarre name?










Have you looked?  Does your distPoint function return non-zero values?

Hi Ian,

It does return non zero values. In fact I can see that dist variable
gets accumulated. But when I call mydist.totalDist(), I get a zero!
Sorry for bizzare names.. :)
suresh
 
S

suresh

Because, you don't change it.


Not everytime. However, for_each() takes the functor argument by value.
Thus, a copy is created. Luckily, it returns the copy that is used
internally. Thus:

  mydist = for_each( f.begin(), f.end(), mydist );

should do the trick (provided, the type has a correctly working assignment
operator).

Best

Kai-Uwe Bux

Hi,
Thank you!
It worked.....so for each takes a copy of the object....and I was
querying the original object!!!
Thanks again
suresh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top