IOS中的Block在C++中的运用

1.iOS中block基本demo

A视图->B视图,B视图传值给A视图

A视图的代码片段

  1. - (IBAction)action2OtherView:(id)sender  
  2. {  
  3.     MyView *myView = [[MyView alloc] init];  
  4.     myView.func = ^(int x,int y)  
  5.     {  
  6.         int xy = myView.number;  
  7.         NSLog(@"xy->%d",xy);  
  8.         [myView removeFromSuperview];  
  9.         return 0;  
  10.     };  
  11.     [self.view addSubview: myView];  
  12.     [myView release];  
  13. }  



B视图:

.h文件:

  1. @interface MyView : UIView  
  2.   
  3. @property(assign,nonatomic) int number;  
  4. @property(copy,nonatomic) int (^func)(int x,int y);  
  5. @property(retain, nonatomic) UIButton* button;  
  6. @end  
  7.   
  8. .m文件:  
  9. - (id)initWithFrame:(CGRect)frame  
  10. {  
  11.     self = [super initWithFrame:frame];  
  12.     if (self) {  
  13.     [self setFrame:CGRectMake(100, 100, 300, 200)];  
  14.     self.backgroundColor = [UIColor yellowColor];  
  15.         // Initialization code  
  16.     self.number = 5;  
  17.           
  18.     self.button  = [UIButton buttonWithType : UIButtonTypeRoundedRect];  
  19.     [self.button setBackgroundColor : [UIColor blueColor]];  
  20.     [self.button setFrame : CGRectMake(50, 50, 50, 50)];  
  21.           
  22.     [self.button addTarget:self action:@selector(showBack) forControlEvents:UIControlEventTouchUpInside];  
  23.     [self addSubview:self.button];  
  24.     }  
  25.     return self;  
  26. }  
  27.   
  28.   
  29. - (void) showBack  
  30. {  
  31.     self.func(5,6);  
  32. }  
  33. - (void)dealloc  
  34. {  
  35.     self.func = nil;  
  36.     self.button = nil;  
  37.     [super dealloc];  
  38. }  


IOS中的block相当于是一个在堆上的代码内存块,是需要释放的


2.C++的纳姆大表达式相当于block的实现

A视图:

  1. void MyView::pageSwitch()  
  2. {  
  3.     SecondView* spSecondView = new SecondView;  
  4.     spSecondView->func = [](int x, int y)  
  5.     {  
  6.         return x + y;  
  7.     };  
  8.     spSecondView->func(10, 20);  
  9. }  
  10.   
  11. B视图:(一定要用functional去包装)  
  12. #ifndef __testBlockCPlusPlus__SecondView__  
  13. #define __testBlockCPlusPlus__SecondView__  
  14.   
  15.   
  16. #include <functional>  
  17.   
  18. class SecondView  
  19. {  
  20. public:  
  21.    SecondView(){};  
  22.    virtual ~SecondView(){};  
  23.    std::function<int(intint)> func;  
  24.       
  25. private:  
  26.    int originX;  
  27.    int originY;  
  28. };  

c++中纳姆大表达式最坑爹的地方,栈对象的地址问题:

  1. #include <iostream>  
  2. #include <functional>  
  1. void foo()  
  2. {  
  3.     int a = 1;  
  4.     int b = 2;  
  5.     int c = 3;  
  6.       
  7.     f = [&]() {  
  8.         cout << a << endl;  
  9.         cout << b << endl;  
  10.         cout << c << endl;  
  11.     };  
  12. }  
  13.   
  14. void bar()  
  15. {  
  16.     int x = 10;  
  17.     int y = 20;  
  18.     int z = 30;  
  19.       
  20.     f();  
  21.       
  22.     x = y = z = 0;  
  23. }  
  24.   
  25. int main()  
  26. {  
  27.     foo();  
  28.     bar();  
  29.       
  30.     return 0;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值