c++可调用类型
1、函数名、函数指针、函数引用
#include <iostream>
void show(int x)
{
std::cout << x << std::endl;
}
int main()
{
show(2); (*show)(2)
void (*p)(int) = show; p(3);
void (&r)(int) = show; r(4);
return 0;
}
2、 成员函数指针
#include <iostream>
class A
{
public:
void show(int a)
{
std::cout << a << std::endl;
}
};
int main()
{
void (A:: *pm)(int) = &A::show;
A a;
(a.*pm)(3);
return 0;
}
3、仿函数
struct A { void operator()(int x) { cout << x << endl; } };
A a; a(23);
4、lambda表达式
[]() {return 0; };
class __lambda_11_5 {
public:
inline int operator()() const {
return 0;
}
using retType_11_5 = int (*)();
inline operator retType_11_5 () const noexcept{
return __invoke;
};
private:
static inline int __invoke()
{
return 0;
}
} __lambda_11_5{};
5、 一个可被转换为函数指针的类对象
void show(int x) { cout << "show:" << x << endl; }
struct A {
using pFunc = void(*)(int);
operator pFunc() { return show; }
};
struct B {
using rFunc = void(&)(int);
operator rFunc() { return show; }
};
A a; a(4);
B b; b(5);