简体   繁体   English

重载运算符功能==

[英]overloading operator function==

I'm not familiar with C++, and my instructor provided a function that i'm confused about 我不熟悉C ++,我的讲师提供了一个我很困惑的功能

     operator long (void);
      long operator == (Base & base) {
       return ! strcmp (name, base);
          }

As far as i know, the operator is doing a comparison on 2 Base objects? 据我所知,操作员正在对2个基础对象进行比较? Or am i wrong? 还是我错了? When i call the function itself, its telling me that there is no such function call. 当我调用函数本身时,它告诉我没有这样的函数调用。

I call the function it returns this error 我调用该函数将返回此错误

no matching function for... operator(Base*&,Base&) 没有匹配的函数...运算符(Base *&,Base&)

The function isn't named operator , it's named operator== . 该函数未命名为operator ,而是命名为operator== It's an overloaded comparison operator . 这是一个重载的比较运算符 You just call it like this: 您可以这样称呼它:

Base a, b;
if(a==b) // <-- this is the function call
   std::cout<<"equal"<<std::endl;
else
   std::cout<<"not equal"<<std::endl;

Of course this is the case when the function is a member of the Base class. 当然,当函数是Base类的成员时就是这种情况。 You didn't provide all of the code, so I'm guessing it is. 您没有提供所有代码,所以我想是的。

On top of that, the 1st line of your code is a declaration of another overloaded operator (one that converts the class to long ), and it's implementation is provided somewhere else (probably). 最重要的是,代码的第一行是另一个重载运算符的声明(该运算符将类转换为long ),并且它的实现在其他地方(可能是)提供。

There are two ways overloading an operator. 重载操作符有两种方法。

1.It can be a member of a class or 1.它可以是班级成员

2.It can be outside class 2.可以在课外

Way of calling the overloaded operator function depends and vary on which way you use. 调用重载运算符的方式取决于您使用的方式。 In your case it seems to be inside class but i suppose there is something wrong in declaration and its not properly declared. 在您的情况下,它似乎在类内部,但我想声明中有错误,并且未正确声明。 Check this question link might be useful Operator overloading outside class 检查此问题链接可能对操作员有用

long operator == (Base &base); // this should be your declaration inside your class

//definition
long operator == (Base &base){
  return !strcmp(name,base.name); 
}

and you can call it over your class' object simply by 您可以通过以下方式在类的对象上调用它:

obj1==obj2 or obj1.operator==(obj2)

think this is useful 认为这很有用

You can define this operator in two ways. 您可以通过两种方式定义此运算符。 First is inside your Base class. 首先是在您的基类内部。

class Base
{
    public:
    long operator==(Base &base);
}

long Base::operator==(Base &base)
{
    ...
}

and the second is outside any class, 第二个是在任何阶级之外

long operator==(Base &left, Base &right)
{
    ...
}

Note that the one declared outside any class must take two parameters, and the one inside the class must take just one. 请注意,在任何类外部声明的一个必须带两个参数,而在类内部声明的一个只能带一个参数。 When you call it like this, 当你这样称呼它时,

base1 == base2;

If you use the first version, base1 is the object in which the operator is called, and base2 is passed as the parameter. 如果使用第一个版本,则base1是在其中调用运算符的对象,并且base2作为参数传递。

If you use the second version, base1 is passed as left, base2 as right. 如果使用第二个版本,则将base1作为左侧传递,将base2作为右侧传递。

From your error message, I suppose you tried to use the second type as I wrote in this example as if it was of the first type. 从您的错误消息中,我想您试图像在本示例中编写的那样尝试使用第二种类型,就好像它是第一种类型一样。

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

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