简体   繁体   English

为什么在使用指针时不要求运算符重载为“ <”?

[英]Why is my operator overload not called for “<” when I use pointers?

When I overload the "==" and "!=" operators I am passing pointers as parameters and the overloaded functions are called and I get the results that I expect but in debugging I discovered that during my call cout << (fruit1 < fruit); 当我重载"==""!="运算符时,我将指针作为参数传递,并且重载的函数被调用,并且得到了期望的结果,但是在调试中,我发现在调用cout << (fruit1 < fruit); , my overloaded "<" method is not being called. ,未调用我的重载"<"方法。 Why is the "<" operator the only one not being overloaded? 为什么"<"运算符是唯一未重载的运算符? I have passed a reference parameter instead to test it and de-referenced fruit and fruit1 in the function call and it worked so the function itself works. 我已经传递了一个引用参数来对其进行测试,并在函数调用中取消引用了fruitfruit1 ,它可以正常工作,因此函数本身可以正常工作。 Is it a property of those individual operators or the fact that the "!=" and "==" methods are inline that is allowing them to work? 是这些单独的运算符的属性还是"!=""=="方法是内联方法允许他们工作?

CPP CPP

#include "Fruit.h" #include“ Fruit.h”

using namespace std;
Fruit::Fruit(const Fruit &temp )
{
    name = temp.name;
    for(int i = 0; i < CODE_LEN - 1; i++)
    {
        code[i] = temp.code[i];
    }
}
bool  Fruit::operator<(const Fruit *tempFruit)
{
    int i = 0;
    while(name[i] != NULL && tempFruit->name[i] != NULL)  
    {
        if((int)name[i] < (int)tempFruit->name[i])
            return true;
        else if((int)name[i] > (int)tempFruit->name[i])
            return false;
        i++;
    }
    return false;
}
std::ostream & operator<<(std::ostream &os, const Fruit *printFruit)
{
    int i = 0;
    os << setiosflags(ios::left) << setw(MAX_NAME_LEN) << printFruit->name << " ";
    for(int i = 0; i < CODE_LEN; i++)
    {
        os << printFruit->code[i];
    }
    os << endl;
    return os;
}

std::istream & operator>>(std::istream &is, Fruit *readFruit)
{

    string tempString;
    is >> tempString;
    int size = tempString.length();
    readFruit->name = new char[tempString.length()];
    for(int i = 0; i <= (int)tempString.length(); i++)
    {
        readFruit->name[i] = tempString[i];
    }
    readFruit->name[(int)tempString.length()] = '\0';
    for(int i =0; i < CODE_LEN; i++)
    {
        is >> readFruit->code[i];
    }
    return is;
}
void main()
{
    Fruit *fruit = new Fruit();
    Fruit *fruit1 = new Fruit();
    cin >> fruit;
    cin >> fruit1;
    cout << (fruit == fruit1);
    cout << (fruit != fruit1);
    cout << (fruit1 < fruit);
    cout << "...";  
}

H H

#ifndef _FRUIT_H
#define _FRUIT_H
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include "LeakWatcher.h"
enum { CODE_LEN = 4 }; 
enum { MAX_NAME_LEN = 30 };
class Fruit
{
private:
    char *name;
    char code[CODE_LEN];
public:
    Fruit(const Fruit &temp);
    Fruit(){name = NULL;};
    bool operator<(const Fruit *other);
   friend std::ostream & operator<<(std::ostream &os, const Fruit *printFruit);
    bool operator==(const Fruit *other){return name == other->name;};
   bool operator!=(const Fruit *other){return name != other->name;};
    friend std::istream & operator>>(std::istream& is, Fruit *readFruit);
};

#endif

Your operator< is a member function, which means that it works for types Fruit, const Fruit* , and you try to pass it Fruit*, Fruit* . 您的operator<是成员函数,这意味着它适用于类型Fruit, const Fruit* ,并且您尝试将其传递给Fruit*, Fruit*

When you declare an operator as a member function then the left parameter is implied to be Fruit . 当您将运算符声明为成员函数时,则left参数隐含为Fruit If you want something else, then you have to make a global operator. 如果您还需要其他东西,则必须创建一个全局运算符。 Unfortunately, you need a class or enumerated type as a parameter, so you can't have two pointers. 不幸的是,您需要一个类或枚举类型作为参数,因此不能有两个指针。

One way to get around this limitation. 克服此限制的一种方法。 Instead of 代替

cout << (fruit1 < fruit);

use 采用

cout << (*fruit1 < fruit);

I also want you to know that this: 我也想让你知道:

(fruit == fruit1)

compares the pointers not what they point to. 比较指针而不是它们指向的指针 In your case those are two distinct objects, so that pointer comparison will always return false. 在您的情况下,这是两个不同的对象,因此指针比较将始终返回false。

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

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