1.用lua模仿c++的指针:
很简单,c++里的指针的内容就是内存地址。用lua进行模仿,只要用一个全局table模拟内存空间,然后将table下标作为指针地址就行了。
请看代码:
2.进行指针测试:
3.用lua模仿类,并且能够继承重载
代码如下:
4.进行类测试的代码
结果:
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/
很简单,c++里的指针的内容就是内存地址。用lua进行模仿,只要用一个全局table模拟内存空间,然后将table下标作为指针地址就行了。
请看代码:
- _g_pz = {};--模拟内存空间
- function _new(something, somem_type)--新建指针
- something = {};
- m_type = "m_type";
- something[m_type] = somem_type;
- if something.m_type == nil then
- print("Cannot assign the point's type!");
- return -1;
- end;
- if type(_g_pz[something.m_type]) ~= "table" then
- _g_pz[something.m_type] = {};
- end;
- _g_pz[something.m_type][something] = {};
- return something;--返回地址
- end;
- function _delete(something)--删除指针
- if something.m_type == nil then
- print("Cannot get the point's m_type!");
- return -1;
- end;
- tremove(_g_pz[something.m_type], something);
- end;
- function _delete_by_type(something)--删除某一类型的所有指针
- if something.m_type == nil then
- print("Cannot get the point's m_type!");
- return -1;
- end;
- tremove(_g_pz, something.m_type);
- end;
- function _pz_assignment(something, value)--给指针赋值
- _g_pz[something.m_type][something] = value;
- end;
- function _pz_get_value(something)--获取指针的值
- return _g_pz[something.m_type][something];
- end;
- function _pz_get_type(something)--获取指针类型
- return something.m_type;
- end;
- --======================模仿指针操作=================
- cTest = _new(cTest, "classtype1");
- --print(cTest[m_type]);
- _pz_assignment(cTest, "2222");
- cTest2 = _new(cTest2, "classtype1");
- _pz_assignment(cTest2, "2222333");
- print(type(_pz_get_type(cTest)).."/ntype = ".._pz_get_type(cTest));
- print(_pz_get_value(cTest));
- print(type(_pz_get_type(cTest2)).."/ntype = ".._pz_get_type(cTest2));
- print(_pz_get_value(cTest2));
- --====================以上===========================
代码如下:
- function class(typename, base_type)
- --m_base_type = "m_base_type";
- local classtype = _new(classtype, typename);
- local classtemp = {}
- classtemp.m_base_type = base_type;
- classtemp.ctor = 0;--构造函数
- classtemp.dector = 0;--析构函数
- classtemp.quote = 0;
- --m_class = {}
- classtemp.new = function(self, ...)
- m_class = _new(m_class, _pz_get_value(%classtype));
- local m_class_type_temp = %classtype;
- self = _pz_get_value(m_class);
- local foo = function(t,i)
- return _pz_get_value(%m_class_type_temp)[i];
- end;
- --settag(_pz_get_value(m_class),newtag());
- --settagmethod(tag(_pz_get_value(m_class)), "index", foo);
- settag(self,newtag());
- settagmethod(tag(self), "index", foo);
- function _ctor(ctype, self, arg)
- if ctype.m_base_type ~= nil then
- _ctor(ctype.m_base_type, self, arg);
- end;
- ctype.ctor(self, arg);
- end;
- _ctor(%classtemp, self, arg);
- _pz_assignment(m_class, self);
- return self;
- end;
- classtemp.dctor = function()
- %classtype.dector();
- if %classtype.m_base_type ~= nil then
- %classtype.m_base_type.dctor();
- end;
- end;
- if classtemp.m_base_type ~= nil then
- local foo3 = function(t, i)
- return %base_type[i];
- end;
- settag(classtemp,newtag());
- settagmethod(tag(classtemp), "index", foo3);
- end;
- _pz_assignment(classtype,classtemp);
- return _pz_get_value(classtype);
- end;
- --=============================类测试============================
- base_type=class("type1") -- 定义一个基类 base_type
- base_type.x = 0;
- function base_type:ctor(self) -- 定义 base_type 的构造函数
- print("base_type ctor")
- self.x = 55;
- end
- --base_type.ctor = function(x)
- -- print("base_type ctor")
- -- self.x=x
- --end
- function base_type:set_x(self, x)
- self.x = x;
- end;
- function base_type:print_x(self) -- 定义一个成员函数 base_type:print_x
- print(self.x)
- end
- function base_type:hello() -- 定义另一个成员函数 base_type:hello
- print("hello base_type")
- end
- test=class("type2", base_type) -- 定义一个类 test 继承于 base_type
- function test:ctor(self) -- 定义 test 的构造函数
- print("test ctor")
- end
- function test:hello() -- 重载 base_type:hello 为 test:hello
- print("hello test")
- end
- a = base_type.new(a);
- b = base_type.new(b);
- b:set_x(b, 2);
- --a:set_x(a, 1);
- a:print_x(a);
- b:print_x(b);
- a:hello()
- c = test.new(c);
- c:set_x(c, 1);
- c:print_x(c);
- --b=test.new(1)
- --b:print_x()
- --b:hello()
- --================以上============================
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/