用lua4.0模仿c++的指针和类

 1.用lua模仿c++的指针:
     很简单,c++里的指针的内容就是内存地址。用lua进行模仿,只要用一个全局table模拟内存空间,然后将table下标作为指针地址就行了。
    请看代码:
  1. _g_pz = {};--模拟内存空间
  2. function _new(something, somem_type)--新建指针
  3.     something = {};
  4.     m_type = "m_type";
  5.     something[m_type] = somem_type;
  6.     if something.m_type == nil then
  7.         print("Cannot assign the point's type!");
  8.         return -1;
  9.     end;
  10.     if type(_g_pz[something.m_type]) ~= "table" then
  11.         _g_pz[something.m_type] = {};
  12.     end;
  13.     _g_pz[something.m_type][something] = {};
  14.     
  15.     return something;--返回地址
  16. end;
  17. function _delete(something)--删除指针
  18.     if something.m_type == nil then
  19.         print("Cannot get the point's m_type!");
  20.         return -1;
  21.     end;
  22.     tremove(_g_pz[something.m_type], something);
  23. end;
  24. function _delete_by_type(something)--删除某一类型的所有指针
  25.     if something.m_type == nil then
  26.         print("Cannot get the point's m_type!");
  27.         return -1;
  28.     end;
  29.     tremove(_g_pz, something.m_type);
  30. end;
  31. function _pz_assignment(something, value)--给指针赋值
  32.     _g_pz[something.m_type][something] = value;
  33. end;
  34. function _pz_get_value(something)--获取指针的值
  35.     return _g_pz[something.m_type][something];
  36. end;
  37. function _pz_get_type(something)--获取指针类型
  38.     return something.m_type;
  39. end;
           2.进行指针测试:
  1. --======================模仿指针操作=================
  2. cTest = _new(cTest, "classtype1");
  3. --print(cTest[m_type]);
  4. _pz_assignment(cTest, "2222");
  5. cTest2 = _new(cTest2, "classtype1");
  6. _pz_assignment(cTest2, "2222333");
  7. print(type(_pz_get_type(cTest)).."/ntype = ".._pz_get_type(cTest));
  8. print(_pz_get_value(cTest));
  9. print(type(_pz_get_type(cTest2)).."/ntype = ".._pz_get_type(cTest2));
  10. print(_pz_get_value(cTest2));
  11. --====================以上===========================
3.用lua模仿类,并且能够继承重载

代码如下:
  1. function class(typename, base_type)
  2.     --m_base_type = "m_base_type";
  3.     local classtype = _new(classtype, typename);
  4.     local classtemp = {}
  5.     classtemp.m_base_type = base_type;
  6.     classtemp.ctor = 0;--构造函数
  7.     classtemp.dector = 0;--析构函数
  8.     classtemp.quote = 0;
  9.     
  10.     
  11.     --m_class = {}
  12.     
  13.     classtemp.new = function(self, ...)
  14.         m_class = _new(m_class, _pz_get_value(%classtype));
  15.         local m_class_type_temp = %classtype;
  16.         self = _pz_get_value(m_class);
  17.         local foo = function(t,i)
  18.             return _pz_get_value(%m_class_type_temp)[i];
  19.         end;
  20.         --settag(_pz_get_value(m_class),newtag());
  21.         --settagmethod(tag(_pz_get_value(m_class)), "index", foo);
  22.         settag(self,newtag());
  23.         settagmethod(tag(self), "index", foo);
  24.         
  25.         function _ctor(ctype, self, arg)
  26.             if ctype.m_base_type ~= nil then
  27.                 _ctor(ctype.m_base_type, self, arg);
  28.             end;
  29.             ctype.ctor(self, arg);
  30.         end;
  31.         
  32.         
  33.         
  34.         _ctor(%classtemp, self, arg);
  35.         _pz_assignment(m_class, self);
  36.         
  37.         return self;
  38.     end;
  39.     
  40.     classtemp.dctor = function()
  41.         
  42.         %classtype.dector();
  43.         
  44.         if %classtype.m_base_type ~= nil then
  45.             %classtype.m_base_type.dctor();
  46.         end;
  47.         
  48.     end;
  49.     
  50.     if classtemp.m_base_type ~= nil then
  51.         local foo3 = function(t, i)
  52.             return %base_type[i];
  53.         end;
  54.         settag(classtemp,newtag());
  55.         settagmethod(tag(classtemp), "index", foo3);
  56.     end;
  57.     
  58.     _pz_assignment(classtype,classtemp);
  59.     
  60.     return _pz_get_value(classtype);
  61. end;
4.进行类测试的代码
  1. --=============================类测试============================
  2. base_type=class("type1")        -- 定义一个基类 base_type
  3. base_type.x = 0;
  4.  
  5. function base_type:ctor(self)   -- 定义 base_type 的构造函数
  6.     print("base_type ctor")
  7.     self.x = 55;
  8. end
  9. --base_type.ctor = function(x)
  10. --  print("base_type ctor")
  11. --  self.x=x
  12. --end
  13. function base_type:set_x(self, x)
  14.     self.x = x;
  15. end;
  16. function base_type:print_x(self)    -- 定义一个成员函数 base_type:print_x
  17.     print(self.x)
  18. end
  19.  
  20. function base_type:hello()  -- 定义另一个成员函数 base_type:hello
  21.     print("hello base_type")
  22. end
  23. test=class("type2", base_type)  -- 定义一个类 test 继承于 base_type
  24.  
  25. function test:ctor(self)    -- 定义 test 的构造函数
  26.     print("test ctor")
  27. end
  28.  
  29. function test:hello()   -- 重载 base_type:hello 为 test:hello
  30.     print("hello test")
  31. end
  32. a = base_type.new(a);
  33. b = base_type.new(b);
  34. b:set_x(b, 2);
  35. --a:set_x(a, 1);
  36. a:print_x(a);
  37. b:print_x(b);
  38. a:hello()
  39. c = test.new(c);
  40. c:set_x(c, 1);
  41. c:print_x(c);
  42. --b=test.new(1) 
  43. --b:print_x()   
  44. --b:hello()
  45. --================以上============================
结果:
C:/Users/Administrator>f:

F:/>cd/new

F:/new>lua.exe new.lua
base_type ctor
base_type ctor
0
2
hello base_type
base_type ctor
test ctor
1

F:/new>



具体脚本文件以及lua编译器下载链接如下:
该死的csdn抽风了
http://download.csdn.net/source/822003
这个链接说我上传资源没了……orz

请大家到这里下载吧……orz
http://hanjiangtian.ys168.com/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值