Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何创建委托并将其作为参数在一行中传递_C_Delegates - Fatal编程技术网

C 如何创建委托并将其作为参数在一行中传递

C 如何创建委托并将其作为参数在一行中传递,c,delegates,C,Delegates,我已经实现了一个列表,我还实现了一个ForEach方法,以简化迭代 typedef struct node { struct node* NextNode; void* Data; } Node; typedef struct list { Node* Head; Node* Tail; int Length; } List; void ListForEach(List* list, void(*iteratorDelegate)(Node*)) {

我已经实现了一个
列表
,我还实现了一个
ForEach
方法,以简化迭代

typedef struct node
{
    struct node* NextNode;
    void* Data;
} Node;

typedef struct list
{
    Node* Head;
    Node* Tail;
    int Length;
} List;

void ListForEach(List* list, void(*iteratorDelegate)(Node*))
{
    Node* actual = list->Head;
    while (actual)
    {
        iteratorDelegate(actual);
        actual = actual->NextNode;
    }
}

Node* NodeNew(void* data)
{
    Node* node = malloc(sizeof(Node));
    node->NextNode = NULL;
    node->Data = data;
    return node;
}

List* ListNew()
{
    List* list = malloc(sizeof(List));
    list->Head = NULL;
    list->Tail = NULL;
    list->Length = 0;
    return list;
}
我的测试程序如下所示:

void PrintDelegate(Node* node)
{
    printf("Data: %d\n", (int)node->Data);
}

int main(int argc, const char* argv[])
{
    List* list = ListNew();
    int counter = 0;
    for (counter; counter != 500000; counter++)
    {
        ListAdd(list, NodeNew((void*)counter));
    }

    ListForEach(list, PrintDelegate);
}
ListForEach(list, 
    [](Node* node) {
        printf("Data: %d\n", (int)node->Data);
    } // end of lambda expression
);
但是使用
ListForEach
感觉很不舒服,因为我总是要创建一个新方法。有没有更简单的方法来委派方法? 在C#中,我会:

ListForEach(list, (node) => printf("Data: %d\n", (int)node.Data));
这也会创建一个委托,但我不需要在类/全局范围中声明新方法


C中也可能出现这种情况吗?

C语言没有lambda函数。但是,C++支持它从C++ 11开始。p> 因此,如果你愿意做出改变,你可以这样做:

void PrintDelegate(Node* node)
{
    printf("Data: %d\n", (int)node->Data);
}

int main(int argc, const char* argv[])
{
    List* list = ListNew();
    int counter = 0;
    for (counter; counter != 500000; counter++)
    {
        ListAdd(list, NodeNew((void*)counter));
    }

    ListForEach(list, PrintDelegate);
}
ListForEach(list, 
    [](Node* node) {
        printf("Data: %d\n", (int)node->Data);
    } // end of lambda expression
);

谷歌搜索“C中的委托是什么”的定义:

C中的委托类似于C或C++中的函数指针。使用 委托允许程序员封装对方法的引用 在委托对象内部。然后可以将委托对象传递给 可以调用引用方法的代码,而不必知道 编译时将调用哪个方法

重点矿山

委托的概念在C中并不存在。C中最接近的是函数指针。使用函数指针,您可以定义几个要指向的方法,但将实际的方法选择推迟到运行时。(即,您不必知道编译时将使用哪种方法。)

一个简单的函数指针示例:

enum {
    ADD,
    SUB,
    MULT,
    DIV,
    OP_MAX
};

typedef float (*pOperation)(float x, float y);

pOperation Op[OP_MAX] = {0}; //create function pointer using typedef

//prototype various functions to use function pointer
float add(float x, float y);
float sub(float x, float y);
float mult(float x, float y);
float div(float x, float y);

int main(void)
{
    float res = 0;
    int i;

    //tie "delegate" to various implementations 
    Op[ADD] = &add;
    Op[SUB] = ⊂
    Op[MULT] = &mult;
    Op[DIV] = ÷

    for(i=ADD;i<OP_MAX;i++)
    {
        //here, Op is used as a delegate for each of the 4 basic arithmetic functions: 
        res = Op[i](3, 7); //performs 4 basic arithmetic functions

        //From definition above: The delegate object can 
        //be passed to code which can call the referenced method, 
        //without having to know at compile time which method 
        //will be invoked
    }

    return 0;
}

//implementations
float add(float x, float y){return x + y;}
float sub(float x, float y){return x - y;}
float mult(float x, float y){return x * y;}
float div(float x, float y){return x / y;}
enum{
添加
附属的,
穆特,
分区,
OP_MAX
};
类型定义浮动(*操作)(浮动x、浮动y);
操作Op[Op_MAX]={0}//使用typedef创建函数指针
//使用函数指针对各种函数进行原型化
浮动添加(浮动x,浮动y);
浮动接头(浮动x、浮动y);
浮点数(浮点数x、浮点数y);
浮动div(浮动x,浮动y);
内部主(空)
{
浮点数res=0;
int i;
//将“委托”绑定到各种实现
Op[ADD]=&ADD;
Op[SUB]=&SUB;
Op[MULT]=&MULT;
Op[DIV]=&DIV;

对于(i=ADD;iYou谈论的是lambda函数。在C@EugeneSh中没有这样的东西。是的,对。我谈论的是lambda函数。但我认为有一种解决方法不需要显式实现该方法。好的,谢谢。知道这一点很好,但它并没有真正帮助我,因为我不可能使用swi到C++的TCH: