功能调用者(回调)用C? [英] Function callers (callbacks) in C?

查看:122
本文介绍了功能调用者(回调)用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想知道他们是如何工作。为了解释什么,我由一个调用函数我的意思是 glutTimerFunc ,它能够把一个函数作为参数,甚至把它称为一个很好的例子的意思它不知道它被宣布。它是如何做呢?

So I was wondering how they work. To explain what I mean by a "function caller" a good example of what I mean would be glutTimerFunc, it is able to take in a function as a parameter and call it even with it not knowing it is declared. How is it doing this?

推荐答案

什么你在谈论被称为的回调并与函数指针来实现用C 并C ++。

What you're talking about is called a callback and is implemented with function pointers in C and C++.

既然你提到过剩,让我们直接坐从 freeglut 源$ C ​​$ C一个真实的例子。我将使用 glutIdleFunc 而不是glutTimerFunc因为code是简单。

Since you mentioned glut, let's take a real example directly from the freeglut source code. I'll use glutIdleFunc instead of glutTimerFunc because the code is simpler.

在供过于求,空闲回调函数(你提供给glutIdleFunc什么)是指向不带任何参数,并没有返回值的函数。 v一个typedef用来给这样的功能类型的名称:

In glut, the idle function callback (what you supply to glutIdleFunc) is a pointer to a function that takes no parameters and returns nothing. A typedef is used to give such a function type a name:

typedef void (* FGCBIdle)( void );

下面,FGCBIdle(简称FreeGlut回调空闲)被定义为一个指针,不带任何参数,其返回类型为void的函数。这仅仅是一个类型定义,使写作前pression容易,它不分配任何存储空间。

Here, FGCBIdle (short for FreeGlut CallBack Idle) is defined as a pointer to a function that takes no parameters and whose return type is void. This is just a type definition that makes writing expression easier, it doesn't allocate any storage.

Freeglut有一个名为SFG_State保存各种设置的结构。该结构的定义之一是:

Freeglut has a structure called SFG_State that holds various settings. Part of the definition of that structure is:

struct tagSFG_State
{
    /* stuff */
    FGCBIdle         IdleCallback;         /* The global idle callback       */
    /* stuff */
};

该结构保持类型FGCBIdle,我们建立了为特定函数指针另一个名称的变量。可以设置IdleCallback字段指向由使用glutIdleFunc函数的用户提供的函数的地址。该函数的(简化)的定义是:

The struct holds a variable of type FGCBIdle, which we established is another name for a specific function pointer. You can set the IdleCallback field to point to the address of a function that is supplied by the user using the glutIdleFunc function. The (simplified) definition of that function is:

void glutIdleFunc( void (* callback)( void ) )
{
    fgState.IdleCallback = callback;
}

下面,fgState是SFG_State变量。正如你所看到的,glutIdleFunc需要一个参数,它是一个函数指针,不带任何参数和返回任何一个函数,这个参数的名字是回调。在函数内部,全局fgState变量内的IdleCallback被设置为用户提供的回调。当调用函数glutIdleFunc,你通过自己的函数(例如glutIdleFunc(myIdle))的名字,但你实际上传递的是函数的地址。

Here, fgState is a SFG_State variable. As you can see, the glutIdleFunc takes one parameter which is a function pointer to a function that takes no parameters and returns nothing, this parameter's name is callback. Inside the function, the IdleCallback inside the global fgState variable is set to the user supplied callback. When you call the glutIdleFunc function, you pass the name of your own function (e.g. glutIdleFunc(myIdle)), but what you're really passing is the address of the function.

后来,glutMainLoop发起的大过剩事件处理循环里面,你会发现这个code:

Later, inside the big glut event processing loop initiated by glutMainLoop, you'll find this code:

if( fgState.IdleCallback )
{
            /* stuff */
            fgState.IdleCallback( );
}

如果一个用户提供空闲的回调是可用的,这就是所谓的循环。如果你在我的文章的开头检查函数指针教程你就会明白语法好,但我希望的一般概念更有意义了。

If a user supplied idle callback is available, it is called in the loop. If you check the function pointer tutorial at the beginning of my post you will understand the syntax better, but I hope the general concept makes more sense now.

这篇关于功能调用者(回调)用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆