简体   繁体   English

递归问题重载运算符

[英]Recursion problem overloading an operator

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 ! 似乎它试图使用这个重载函数本身进行比较,但label_left是一个字符串 ,而不是域名

Any suggestion? 有什么建议吗?

typedef just gives another name for a type. typedef只为类型提供另一个名称。 It does not create a distinct type. 创建一个独特的类型。 So in effect, you're overloading operator < for string . 所以实际上,你正在重载operator < for string

If you want to create a distinct type, then you can try 如果您想创建一个不同的类型,那么您可以尝试

struct domain_name {
   string data;
   // ...
};

and work with that. 并与之合作。

Typedef doesn't work like this. Typedef不能像这样工作。 Typedef simply defines an alias for the type - it is still a string. Typedef只是为类型定义了一个别名 - 它仍然是一个字符串。 In order to do this, you would need a new type instead. 为此,您需要一个新类型。 You should do this anyway. 无论如何你应该这样做。 Your operator is overloading the comparison operator for all strings. 您的运算符正在为所有字符串重载比较运算符。

Your typedef doesn't create a new type. 您的typedef不会创建新类型。 It just creates a new name to refer to the same type as before. 它只是创建一个新名称来引用与以前相同的类型。 Thus, when you use < inside your operator function on two strings, the compiler just uses the same operator it's compiling because the argument types match. 因此,当你在两个字符串的运算符函数内使用<时,编译器只使用它编译的相同运算符,因为参数类型匹配。

What you may wish to do instead is define an entirely new function: 您可能希望做的是定义一个全新的功能:

bool domain_less(domain_name const& left, domain_name const& right);

Then use that function in places that call for a comparison function, such as std::sort . 然后在调用比较函数的地方使用该函数,例如std::sort Most of the standard algorithms will use < by default, but allow you to provide your own predicate function instead. 大多数标准算法将默认使用< ,但允许您提供自己的谓词函数。 You may need to use std::ptr_fun to wrap your function. 您可能需要使用std::ptr_fun来包装您的函数。 You can also write your own functor object; 您也可以编写自己的仿函数对象; it's typical to descend from std::binary_function in that case. 在这种情况下,通常从std::binary_function下降。 (Check out the <functional> header.) (查看<functional>标题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM