简体   繁体   English

&function_name在以下代码中描述了什么

[英]what does &function_name depicts in the following code

I have following code snippet, what does &main and &user depict here? 我有以下代码片段, &main&user在这里描述了什么? Each time I run why does it give different values? 每次运行时,为什么它赋予不同的值? In what scenarios passing &function_name is useful? 在什么情况下,传递&function_name是有用的?

int user()
{
      return 0;  
}

int main()
{
    int a, b = 0;
    a = ((int)&main);
    b = ((int)&user);
    cout << a <<" " << b;
    return 0;
}

It depcits the address of your functions. 它发送您的函数的地址。 The reason why its different each time you run it, is because your program probably does not use the same memory space each time. 每次运行时其不同的原因是,您的程序可能每次都不使用相同的内存空间。

&main gives you the address of your main function, quite like &var gives you the address of a variable called var. &main为您提供主函数的地址,就像&var为您提供名为var的变量的地址一样。 The cast to int is probably problematic. 强制转换为int可能有问题。 Pointers on a 64 bit system are 64 bit wide, an int may not be that long. 64位系统上的指针为64位宽,一个int可能不会那么长。

sbi explained it nicely here : sbi 在这里很好地解释了这一点

Most examples boil down to callbacks : You call a function f() passing the address of another function g() , and f() calls g() for some specific task. 大多数示例都归结为回调 :您调用函数f()传递另一个函数g()的地址,而f()调用g()进行某些特定任务。 If you pass f() the address of h() instead, then f() will call back h() instead. 如果传递f()代替h() f()的地址,则f()将代替回调h()

Basically, this is a way to parametrize a function: Some part of its behavior is not hard-coded into f() , but into the callback function. 基本上,这是一种参数化函数的方法:它的某些行为不是硬编码为f() ,而是硬编码为回调函数。 Callers can make f() behave differently by passing different callback functions. 通过传递不同的回调函数,调用者可以使f()行为不同。 A classic is qsort() from the C standard library that takes its sorting criterion as a pointer to a comparison function. 一个经典的qsort()是C标准库中的qsort() ,它将其排序标准作为指向比较函数的指针。

In C++, this is often done using function objects (also called functors). 在C ++中,通常使用函数对象 (也称为函子)来完成此操作。 These are objects that overload the function call operator, so you can call them as if they were a function. 这些对象使函数调用运算符过载,因此您可以像调用它们一样调用它们。 Example: 例:

 class functor { public: void operator()(int i) {std::cout << "the answer is: " << i << '\\n';} }; functor f; f(42); 

The idea behind this is that, unlike a function pointer, a function object can carry not only an algorithm, but also data: 其背后的想法是,与函数指针不同,函数对象不仅可以承载算法,还可以承载数据:

 class functor { public: functor(const std::string& prompt) : prompt_(prompt) {} void operator()(int i) {std::cout << prompt_ << i << '\\n';} private: std::string prompt_; }; functor f("the answer is: "); f(42); 

Another advantage is that it is sometimes easier to inline calls to function objects than calls through function pointers. 另一个优点是,内联调用函数对象有时比通过函数指针进行调用要容易。 This is a reason why sorting in C++ is sometimes faster than sorting in C. 这就是为什么在C ++中进行排序有时比在C中进行排序更快的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM