Recursion problem overloading an operator
Posted
by Tronfi
on Stack Overflow
See other posts from Stack Overflow
or by Tronfi
Published on 2010-05-04T18:58:02Z
Indexed on
2010/05/04
19:08 UTC
Read the original article
Hit count: 172
c++
|operator-overloading
I have this:
typedef string domanin_name;
And then, I try to overload the operator< in this way:
bool operator<(const domain_name & left, const domain_name & right){
int pos_label_left = left.find_last_of('.');
int pos_label_right = right.find_last_of('.');
string label_left = left.substr(pos_label_left);
string label_right = right.substr(pos_label_right);
int last_pos_label_left=0, last_pos_label_right=0;
while(pos_label_left!=string::npos && pos_label_right!=string::npos){
if(label_left<label_right) return true;
else if(label_left>label_right) return false;
else{
last_pos_label_left = pos_label_left;
last_pos_label_right = pos_label_right;
pos_label_left = left.find_last_of('.', last_pos_label_left);
pos_label_right = right.find_last_of('.', last_pos_label_left);
label_left = left.substr(pos_label_left, last_pos_label_left);
label_right = right.substr(pos_label_right, last_pos_label_right);
}
}
}
I know it's a strange way to overload the operator <, but I have to do it this way. It should do what I want. That's not the point.
The problem is that it enter in an infinite loop right in this line:
if(label_left<label_right) return true;
It seems like it's trying to use this overloading function itself to do the comparision, but label_left is a string, not a domain name!
Any suggestion?
© Stack Overflow or respective owner