你好,游客 登录 注册 搜索
背景:
阅读新闻

现在c语言都这样写了么 - 岳麓听雨专栏

[日期:2013-03-18] 来源:  作者: [字体: ]
//
//  main.c
//  ctest
//
//  Created by liweijian on 13-3-18.
//  Copyright (c) 2013年 liweijian. All rights reserved.
//

#include <stdio.h>
#include <Block.h>

//闭包
typedef void(*func_t)();
func_t test()
{
    void func1()
    {
        printf("%s\n", __func__);
        printf("in func\n");
    };
    return func1;
}

//block1
#define test2() ({  \
    char _a = 'a'; \
    _a++;          \
    _a;            \
})


//block2
typedef int (^IntBlock)();
IntBlock MakeCounter(int start, int increment)
{
    __block int i = start;
    
    return Block_copy( ^ {
        int ret = i;
        i += increment;
        return ret;
    });
    
    
}

int main(int argc, const char * argv[])
{

  
    test()();
    
    int i = test2();
    printf("%d\n", i);
    
    
    IntBlock mycounter = MakeCounter(5, 2);
    printf("First call: %d\n", mycounter());
    printf("Second call: %d\n", mycounter());
    printf("Third call: %d\n", mycounter());
    
    /* because it was copied, it must also be released */
    Block_release(mycounter);
    return 0;
}





收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻