operator_0">C++ operator()
flyfish
operator() 是C++中的一种运算符重载,称为调用运算符>函数调用运算符或调用运算符
仿函数 (Functors):用来封装函数或行为,使对象可以像函数一样调用
以下是一个简单的示例,展示了如何使用 operator()
:
#include <iostream>// 定义一个类
class Functor {
publicoperator">:// 重载调用运算符>函数调用运算符void operator()() {std::cout operator"><< "Functor 被调用了!" operator"><< std::endl;}
};int main() {Functor functor; // 创建 Functor 对象functor(); // 调用对象,就像调用函数一样return 0;
}
operator__31">复杂点的仿函数:定义了一个 MultiplyBy
仿函数类,它的 operator()
运算符将输入的整数乘以一个因子
使用 std::transform
算法将 vec
中的每个元素乘以 3
#include <iostream>
#include <vector>
#include <algorithm>// 定义一个仿函数类
class MultiplyBy {int factor;
publicoperator">:MultiplyBy(int f) operator">: factor(f) {}// 重载调用运算符>函数调用运算符int operator()(int x) const {return x operator">* factor;}
};int main() {std::vectoroperator"><intoperator">> vec operator">= {1, 2, 3, 4, 5};// 使用仿函数将每个元素乘以 3std::transform(vec.begin(), vec.end(), vec.begin(), MultiplyBy(3));for (int val operator">: vec) {std::cout operator"><< val operator"><< " ";}return 0;
}
定义了一个 Event
类,它管理一个回调函数
回调函数是一种通过函数指针或仿函数在特定事件发生时调用的函数。
当 trigger
方法被调用时,它会调用设置的回调函数。使用 Callback
仿函数作为回调函数,并在 main
函数中触发事件。
#include <iostream>
#include <functional>// 定义一个回调管理类
class Event {std::functionoperator"><void()operator">> callback;
publicoperator">:void setCallback(std::functionoperator"><void()operator">> cb) {callback operator">= cb;}void trigger() {if (callback) {callback();}}
};// 定义一个仿函数类
class Callback {
publicoperator">:void operator()() const {std::cout operator"><< "回调函数被调用了!" operator"><< std::endl;}
};int main() {Event event;Callback callback;// 设置回调函数event.setCallback(callback);// 触发事件,调用回调函数event.trigger();return 0;
}
有参数的调用运算符>函数调用运算符
你可以定义带参数的 operator()
,如下所示:
class Adder {int increment;
publicoperator">:Adder(int inc) operator">: increment(inc) {}int operator()(int x) const {return x operator">+ increment;}
};int main() {Adder adder(5);std::cout operator"><< adder(10) operator"><< std::endl; // 输出 15return 0;
}
多个参数的调用运算符>函数调用运算符
调用运算符>函数调用运算符 operator()
的语法要求使用一对括号。内部的参数列表括号可以包含任意数量的参数,但调用运算符>函数调用运算符本身只有一对外括号。
class Multiplier {
publicoperator">:int operator()(int a, int b) const {return a operator">* b;}
};int main() {Multiplier multiplier;std::cout operator"><< multiplier(3, 4) operator"><< std::endl; // 输出 12return 0;
}
重载数组下标运算符
class Array {int data[10];
publicoperator">:Array() {for (int i operator">= 0; i operator">< 10; operator">++i) {data[i] operator">= i;}}intoperator">& operator[](int index) {return data[index];}
};int main() {Array arr;arr[5] operator">= 100; // 使用重载的数组下标运算符std::cout operator"><< "arr[5] = " operator"><< arr[5] operator"><< std::endl; // 输出 arr[5] = 100return 0;
}
算术运算符** (+
, -
, *
, /
)
class Complex {double real, imag;
publicoperator">:Complex(double r, double i) operator">: real(r), imag(i) {}// 重载加法运算符Complex operatoroperator">+(const Complexoperator">& other) const {return Complex(real operator">+ other.real, imag operator">+ other.imag);}void print() const {std::cout operator"><< "(" operator"><< real operator"><< ", " operator"><< imag operator"><< "i)" operator"><< std::endl;}
};int main() {Complex a(1.0, 2.0);Complex b(3.0, 4.0);Complex c operator">= a operator">+ b; // 使用重载的加法运算符c.print(); // 输出 (4.0, 6.0i)return 0;
}
比较运算符** (==
, !=
)
class Complex {double real, imag;
publicoperator">:Complex(double r, double i) operator">: real(r), imag(i) {}// 重载等于运算符bool operatoroperator">==(const Complexoperator">& other) const {return real operator">== other.real operator">&& imag operator">== other.imag;}
};int main() {Complex a(1.0, 2.0);Complex b(1.0, 2.0);if (a operator">== b) {std::cout operator"><< "a and b are equal" operator"><< std::endl;}return 0;
}
输入输出运算符** (<<
, >>
)
#include <iostream>class Complex {double real, imag;
publicoperator">:Complex(double r, double i) operator">: real(r), imag(i) {}// 重载输出运算符friend std::ostreamoperator">& operatoroperator"><<(std::ostreamoperator">& os, const Complexoperator">& c) {os operator"><< "(" operator"><< c.real operator"><< ", " operator"><< c.imag operator"><< "i)";return os;}
};int main() {Complex a(1.0, 2.0);std::cout operator"><< a operator"><< std::endl; // 输出 (1.0, 2.0i)return 0;
}