[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Inserting objects into tables
- From: trader9 <trader9@...>
- Date: Thu, 12 Jun 2008 17:32:54 +0530
Hi,
This is my first post to the lua mailing list.
I have just started learning lua.
Please just check the following code:
****************
ClassA = {}
function ClassA:new(name)
object = {}
setmetatable(object, self)
self.__index = self
self.name = name
return object
end
ClassB = {}
function ClassB:new()
object = {}
setmetatable(object, self)
self.__index = self
self.tabs = {}
return object
end
function ClassB:add(name)
local ser = ClassA:new(name)
table.insert(self.tabs,ser)
end
function ClassB:print()
for i,obj in pairs(self.tabs) do
print(obj.name)
end
end
ob = ClassB:new()
ob:add('Earth')
ob:add('Mars')
ob:print()
****************
Shouldn't the above code print:
Earth
Mars
But it doesn't. It prints
Mars
Mars
What mistake am I making?
Thanks for your help.