【问题标题】:Why is my operator overload not called for "<" when I use pointers?为什么当我使用指针时,我的运算符重载没有调用“<”?
【发布时间】:2013-10-08 01:45:40
【问题描述】:

当我重载 "==""!=" 运算符时,我将指针作为参数传递,重载函数被调用,我得到了我期望的结果,但在调试中我发现在调用 cout &lt;&lt; (fruit1 &lt; fruit); 期间,我的重载函数"&lt;" 方法未被调用。为什么"&lt;" 运算符是唯一没有被重载的?我已经传递了一个引用参数来测试它并在函数调用中取消引用 fruitfruit1 并且它工作正常,所以函数本身工作。是这些单个运算符的属性还是 "!=""==" 方法是内联的这一事实允许它们工作?

CPP

#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

#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

【问题讨论】:

  • 为什么要重载运算符以获取指针而不是引用?

标签: c++ pointers reference operator-overloading


【解决方案1】:

您的operator&lt; 是一个成员函数,这意味着它适用于类型Fruit, const Fruit*,您尝试将它传递给Fruit*, Fruit*

当您将运算符声明为成员函数时,左侧参数隐含为Fruit。如果你想要别的东西,那么你必须做一个全球运营商。不幸的是,你需要一个类或枚举类型作为参数,所以你不能有两个指针。

解决此限制的一种方法。而不是

cout << (fruit1 < fruit);

使用

cout << (*fruit1 < fruit);

我也想让你知道:

(fruit == fruit1)

比较指针而不是它们指向的东西。在您的情况下,它们是两个不同的对象,因此指针比较将始终返回 false。

【讨论】:

  • "那么你必须创建一个全局运算符" -- 除非你不能。重载运算符时,至少有一个操作数需要是用户定义的类型。
  • @BenjaminLindley 感谢您的更正,直到现在我才意识到这个限制。但这是有道理的,重载指针比较可能会导致破坏。
  • 那么如果不首先取消引用对象就没有办法进行这种比较(无论如何都不会让人头疼)?为什么这些规则不适用于"==" 重载?我的意思是,它不也是Fruit, const Fruit*类型的成员函数吗?
  • @CraigPatrickLafferty == 是指针比较,而不是您的重载。请参阅我的编辑。您可以编写一个常规函数,例如称为 less_than(const Fruit*, const Fruit*) 并改用它。
  • 我明白了。我没有对它进行足够彻底的测试,结果我实际上并没有得到"==""!=" 运算符的正确结果。所以它们都不起作用。
猜你喜欢
  • 2011-01-21
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 2020-12-18
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
相关资源
最近更新 更多