简体   繁体   English

在 C++ 中调用类中的运算符函数

[英]Calling an operator function in a class in C++

Learning C++, I got an operator function in a class but don't know how to call it:学习C++,我在一个类中得到了一个运算符函数,但不知道如何调用它:

class Getbytes
{
public:
      Getbytes();

      int operator() (unsigned char* C1, unsigned char* C2)
       {do something to C1 and C2 and return int; };
}

main ()
{

 Getbyes myBytes;

//Here, how to call the "operator() (unsigned char* C1, unsigned char*C2)"?
 myBytes??  

}

You call it as myBytes();你称之为myBytes(); or myBytes.operator();myBytes.operator(); if you want to be verbose about it.如果你想详细说明它。

Of course you also need to pass the arguments the function needs.当然,您还需要传递函数所需的参数。 Like myBytes("foo", "bar");myBytes("foo", "bar");

You can call it like你可以称之为

Getbytes myBytes;

unsigned char s1[] = "Hello";
unsigned char s2[] = "World";

myBytes( s1, s2 );

or或者

myBytes.operator()( s1, s2 );

If the operator does not change the object itself of the class Getbytes then it should be declared like如果运算符不更改 Getbytes 类的对象本身,则应将其声明为

int operator() (unsigned char* C1, unsigned char* C2) const;

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

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