随笔-341  评论-2670  文章-0  trackbacks-0
    现在不仅可以阅读类的成员,也可以用函数名的字符串去调用函数并取得结果了。代码与这篇文章的实例类似,因此只贴出更改的部分以及程序截图。

    首先为Human类添加了成员函数:
 1     void PrintHello()
 2     {
 3         GetConsole()->WriteLine(L"Hello, I\'m "+Name+L".");
 4     }
 5 
 6     VUnicodeString GetName()
 7     {
 8         return Name;
 9     }
10 
11     VL_AutoPtr<Animal> GetPat(VInt Index)
12     {
13         return OtherPats[Index];
14     }
15 
16     Bird GetTransportation(VInt Index)
17     {
18         return OtherTranspotationsList[Index];
19     }
20 
21     VBool IsAllBird(VL_AutoPtr<Animal> aAnimal , VInt Index)
22     {
23         return VL_AutoPtr<Bird>(aAnimal) && VL_AutoPtr<Bird>(OtherPats[Index]);
24     }
25 
26     void PrintIsAllBird(VL_AutoPtr<Animal> aAnimal , VInt Index)
27     {
28         GetConsole()->WriteLine(IsAllBird(aAnimal,Index)?L"all bird":L"not all bird");
29     }

    其次是注册函数的代码:
 1     VL_BEGIN_SUB_CLASS(Human,Mammalian)
 2         VL_ADD_CLASS_MEMBER(Name)
 3         VL_ADD_CLASS_MEMBER(MainPat)
 4         VL_ADD_CLASS_MEMBER(MainTransportation)
 5         VL_ADD_CLASS_MEMBER(OtherPats)
 6         VL_ADD_CLASS_MEMBER(OtherTranspotations)
 7         VL_ADD_CLASS_MEMBER(OtherPatsList)
 8         VL_ADD_CLASS_MEMBER(OtherTranspotationsList)
 9         VL_ADD_CLASS_MEMBER(PatMap)
10         VL_ADD_CLASS_MEMBER(PatMultiMap)
11         VL_ADD_CLASS_METHOD(PrintHello)
12         VL_ADD_CLASS_METHOD(GetName)
13         VL_ADD_CLASS_METHOD(GetPat)
14         VL_ADD_CLASS_METHOD(GetTransportation)
15         VL_ADD_CLASS_METHOD(IsAllBird)
16         VL_ADD_CLASS_METHOD(PrintIsAllBird)
17     VL_END_CLASS(Human)

    然后是调用函数的代码。注意我们使用函数名进行调用,参数全部使用指针,而且调用完会自动释放:
 1 void PrintType(VL_InspectorManager::Ptr Manager , VL_ObjectType::Ptr Type)
 2 {
 3     VL_InspectorConverterManager Converter;
 4     Converter.AddNormalConverter(new VL_InspectorReader);
 5 
 6     VL_ObjectInspector::Ptr Inspector=Manager->GetInspector(L"VL_ObjectType");
 7     GetConsole()->WriteLine(Converter.FindToStringConverter(Inspector)->ConvertToString(Inspector,Type.Object()));
 8 }
 9 
10 //下面这里是main函数中的代码片段
11     {
12         VL_AutoPtr<VL_StructInspector> HumanInspector=Manager->GetInspector(L"Human");
13         {
14             GetConsole()->WriteLine(L"开始调用函数:PrintHello");
15             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"PrintHello");
16             VL_List<VPointer , true> Parameters;
17             PrintResult(Method->Invoke(&human,Parameters));
18         }
19         {
20             GetConsole()->WriteLine(L"开始调用函数:GetName");
21             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"GetName");
22             VL_List<VPointer , true> Parameters;
23             PrintResult(Method->Invoke(&human,Parameters));
24         }
25         {
26             GetConsole()->WriteLine(L"开始调用函数:GetPat");
27             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"GetPat");
28             VL_List<VPointer , true> Parameters;
29             Parameters.Add(new int(0));
30             PrintResult(Method->Invoke(&human,Parameters));
31         }
32         {
33             GetConsole()->WriteLine(L"开始调用函数:GetTransportation");
34             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"GetTransportation");
35             VL_List<VPointer , true> Parameters;
36             Parameters.Add(new int(0));
37             PrintResult(Method->Invoke(&human,Parameters));
38         }
39         {
40             GetConsole()->WriteLine(L"开始调用函数:IsAllBird");
41             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"IsAllBird");
42             VL_List<VPointer , true> Parameters;
43             Parameters.Add(new Bird);
44             Parameters.Add(new int(0));
45             PrintResult(Method->Invoke(&human,Parameters));
46         }
47         {
48             GetConsole()->WriteLine(L"开始调用函数:PrintIsAllBird");
49             VL_MethodInspector::Ptr Method=HumanInspector->GetMethod(L"PrintIsAllBird");
50             VL_List<VPointer , true> Parameters;
51             Parameters.Add(new Bird);
52             Parameters.Add(new int(2));
53             PrintResult(Method->Invoke(&human,Parameters));
54         }
55     }

    最后是程序截图:

    于是可以开始做很多好玩的事情了。
posted on 2009-06-29 20:47 陈梓瀚(vczh) 阅读(4758) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理