c++ operator overloading. Function to get private value from overload
I have trouble getting the "print" function in this class to get the right
total
class PRN {
private:
typedef pair < string, int > P;
int sz, // map size – no of distinct words
cnt, // counter for printing
total; // no of words
public:
// constructor
PRN ( const int& s = 1, const int& c = 0, const int& t = 0 ){
cnt= c;
total = t;
sz = s;
}
void operator ( ) ( const P& p ){
total += p.second;
cout << total;
}// overloaded operator, where P is defined as
// typedef pair < string, int > P;
void print () const{
cout <<"no of words in output list : " << total << endl;
}
};
then in my main i call
PRN p (m.size());
for_each(m.begin(),m.end(),p);
p.print();
m is a map containing some values(string, int); The operator is adding
because im printing them and I can see they are getting added but when I
called p.print() it returns zero for the "total".
Any suggestions? Thanks
This comment has been removed by the author.
ReplyDelete