V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Akagi201
V2EX  ›  问与答

如何理解和使用闭包? 最好用 C 语言描述

  •  
  •   Akagi201 · Dec 1, 2014 · 3368 views
    This topic created in 4173 days ago, the information mentioned may be changed or developed.
    今天看到一个小的C的项目, https://github.com/clibs/commander, 上面写到

    Closure

    cmd.data is a void * so pass along a struct to the callbacks if you want.

    这个项目的作者是搞node跟js的, 可能引进了一些思想到C来, 不过我没有看懂这怎么就叫闭包了?

    在我看来, 只是回调函数的参数里有个成员是个空指针, 可以传任何值. 这样就算闭包吗?
    5 replies    2014-12-01 18:23:10 +08:00
    GuangXiN
        1
    GuangXiN  
       Dec 1, 2014
    C语言不支持闭包吧
    ggarlic
        2
    ggarlic  
       Dec 1, 2014   ❤️ 1
    回调参数中的空指针只能算是“类似”闭包
    gcc对c作了扩展来支持闭包,名叫Nested Functions
    https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html
    1989922yan
        3
    1989922yan  
       Dec 1, 2014   ❤️ 1
    作用域扩大的一种方法。
    Akagi201
        4
    Akagi201  
    OP
       Dec 1, 2014
    @1989922yan 有点这种感觉
    tabris17
        5
    tabris17  
       Dec 1, 2014
    闭包的核心是upval
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2903 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 09:08 · PVG 17:08 · LAX 02:08 · JFK 05:08
    ♥ Do have faith in what you're doing.