WINCE(WM5.0 PPC)下Loki的测试——Functor

本文介绍了如何在Windows CE环境下使用Loki库的Functor,包括用函数、functor对象、类成员函数以及Loki的Chain函数来初始化Functor,并展示了执行这些Functor的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文演示一个在WINCE环境下Loki之Functor模板类的测试例子。主要演示了下面几种Functor的用法:
  • 用函数初始化Loki::Functor
  • 用functor初始化Loki::Functor
  • 用类成员函数初始化Loki::Functor
  • 用其它Loki::Functor初始化Loki::Functor
  • 用Loki的串接函数(Chain)初始化Loki::Functor
这些例子主要来源于书中(Modern C++ Design)的示例代码,本人添加了一些实现代码,以及对Loki-0.1.6版下的适应性修改。可到sf.net下载Loki-0.1.6(目前的最新版)。

下面贴出示例代码:
  1. #include "stdafx.h"  //
  2. #include <Loki/Functor.h>
  3. #include <Loki/TypelistMacros.h>
  4. #include <iostream>
  5. //using namespace Loki;
  6. // a function
  7. void Function(int m)
  8. {
  9.     std::cout << " void Function(" << m << "); " << std::endl;
  10. }
  11. // a functor
  12. struct SomeFunctor {
  13.     void operator()(int m) {
  14.         std::cout << " SomFunction::operator()(" << m << ");" << std::endl;
  15.     }
  16. };
  17. // a class member function
  18. struct SomeClass {
  19.     void MemberFunction(int m) {
  20.         std::cout << "SomeClass::MemberFunction(" << m << "); " << std::endl;
  21.     }
  22. };
  23. void testFunctors()
  24. {
  25.     // Initialize with a function
  26.     Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd1(Function);
  27.     
  28.     // Initialize with a functor
  29.     SomeFunctor fn;
  30.     Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd2(fn);
  31.     // Initialize with a pointer to member function
  32.     // and a pointer to member function
  33.     SomeClass myObject;
  34.     Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd3(&myObject, &SomeClass::MemberFunction);
  35.     // Initialize a functor with another 
  36.     // (Copying)
  37.     Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd4(cmd3);
  38.     Loki::Functor<void, LOKI_TYPELIST_1(int)> cmd5(Chain(cmd1, cmd2));
  39.     // run
  40.     std::cout << "call cmd1()..." << std::endl;
  41.     cmd1(1);
  42.     std::cout << "call cmd2()..." << std::endl;
  43.     cmd2(2);
  44.     std::cout << "call cmd3()..." << std::endl;
  45.     cmd3(3);
  46.     std::cout << "call cmd4()..." << std::endl;
  47.     cmd4(4);
  48.     std::cout << "call cmd5()..." << std::endl;
  49.     cmd5(5);
  50. }
运行结果如下:
call cmd1()...
 void Function(1);
call cmd2()...
 SomFunction::operator()(2);
call cmd3()...
SomeClass::MemberFunction(3);
call cmd4()...
SomeClass::MemberFunction(4);
call cmd5()...
 void Function(5);
 SomFunction::operator()(5);

以上例子在模拟器下运行通过,由于在编译时已经指定指令集(instructionset)为ARMVI,相信也可以在真机上运行。

另,S60 3rd Ed以后的版本均支持OpenC/C++,过一段时间得空时将给出Symbian下的测试结果(模拟器/真机)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值