void f(string name) {
cout << "f()->my name is :" << name << endl;
}
int main() {
f("1");
auto i = f;
i("2");
}
鼠标放在i上面可以看到类型,所以还可以这样:
void(*j)(string) = f;
j("2");
typedef void(*m)(string);
m a=f; a("4");
using n = void(*)(string);
n b=f; b("5");
接下来讲一下成员函数
class person {
friend int main();
private:
void static f()
{
cout << "person->f()" << endl;
}
};
main: //函数一定要是静态的,因为静态成员不属于类
person per;
auto i = per.f;
auto j = person::f;
i();
j();
//typedef 和 using还没有研究出来
看如下伪代码
void f(int value)
{
std::cout << "value:" << value << std::endl;
}
void function(const std::vector<int>V,void(*fun)(int))
{
for (int i : V)
fun(i);
}
main:
std::vector<int>V = { 1,2,3,4,5 };
function(V, f);
还可以更简单一点(涉及到lambda匿名函数)
void function(const std::vector<int>V,void(*fun)(int))
{
for (int i : V)
fun(i);
}
main:
std::vector<int>V = { 1,2,3,4,5 };
function(V, [](int value) {std::cout << "value:" << value << std::endl; });
//不用像普通函数一样要声明,在代码过程中实现,用完就丢。不算真正的函数