gpt4 book ai didi

c++ - () 的运算符重载及其使用的不同方式和条件

转载 作者:行者123 更新时间:2023-11-28 00:52:36 25 4
gpt4 key购买 nike

我想知道如何重载 () 运算符

例如

class A{
int a,b;
public:
void operator()(int x, int y);
};

void A::operator()(int x, int y){
x = a;
y = b;
}

int main(){
A a = new A();
a(5,4); // this will call the overloaded operator
return 0;
}

我想知道以下是否还有其他用例以及可以调用它的任何其他场景。

最佳答案

operator() 就像任何其他函数一样。唯一的区别是,您不是像 a.foo() 那样调用它,而是编写 a()

你可以像使用任何其他函数一样使用它,你可以根据需要重载它,它可以有你想要的任意多的参数(包括零),它可以被模板化以及你能想到的任何其他东西任何其他功能都可以。

关于c++ - () 的运算符重载及其使用的不同方式和条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com