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

Calling an operator function in a class in C++

本文关键字:运算符 函数 调用 C++      更新时间:2023-10-16

学习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??  
}

如果你想详细地说,你可以把它称为myBytes();myBytes.operator();

当然,您还需要传递函数所需的参数。喜欢myBytes("foo", "bar");

你可以这样称呼它

Getbytes myBytes;
unsigned char s1[] = "Hello";
unsigned char s2[] = "World";
myBytes( s1, s2 );

myBytes.operator()( s1, s2 );

如果运算符不更改类 Getbytes 的对象本身,则应将其声明为

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