lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm looking for a solution/workaround/explanation for the following
inheritance problem:

This test code assumes the settagmethod and clone functions are initialized
and defined...

-- Define counter Object
counter_base = {}
function counter_base:new()
	self.parent = nil
	self.num = 0
end
function counter_base:inc()
	self.num = self.num + 1
end
function counter_base:out()
	print (format("%03d ",self.num))
end

-- Implement
counter_base:new()
counter_base:out()
counter_base:inc()	-- inc() works just fine here
counter_base:out()	

print ("-------Child Object-------")

-- Clone a child object and further define it
counter = clone(counter_base)
function counter:new()
	self.parent = counter_base
end
function counter:child_inc()
	self.parent:inc()		-- here's the problem code line
	-- Other code
	print ("Wazoo")
end

-- Implement
counter:new()
counter:out()
counter:inc()		-- this works fine
counter:out()
counter:child_inc()	-- but this works on counter_base's data, not
counter's
counter:out()


Am I overlooking something?  Is this expected behaviour?  How can I make
child_inc() work on it's own data?

Thanks very much,

Pete Gardner
Helikon Technologies Inc.
http://www.helikon.com