Function pointers, Closures, and Lambda

前端 未结 12 1450
臣服心动
臣服心动 2020-11-28 01:51

I am just now learning about function pointers and, as I was reading the K&R chapter on the subject, the first thing that hit me was, \"Hey, this is kinda like a closure

12条回答
  •  盖世英雄少女心
    2020-11-28 02:56

    Most of the responses indicate that closures require function pointers, possibly to anonymous functions, but as Mark wrote closures can exist with named functions. Here's an example in Perl:

    {
        my $count;
        sub increment { return $count++ }
    }
    

    The closure is the environment that defines the $count variable. It is only available to the increment subroutine and persists between calls.

提交回复
热议问题