Data inheritance problem

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Data inheritance problem

Pete Gardner
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




Reply | Threaded
Open this post in threaded view
|

Re: Data inheritance problem

Steve Dekorte-6
Pete Gardner wrote:
>counter:child_inc()	-- but this works on counter_base's data, not counter's...
> Am I overlooking something?  Is this expected behaviour?  How can I make 
> child_inc() work on it's own data? 

If you want it to work on the child you need to change:
    function counter:child_inc() self.parent:inc() end
to:
    function counter:child_inc() self.parent.inc(self) end
or more simply:
    function counter:child_inc() self:inc() end

If you implement a method lookup tag on your objects.

Steve

Reply | Threaded
Open this post in threaded view
|

RE: Data inheritance problem

Pete Gardner
In reply to this post by Pete Gardner
Thank you!

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




> -----Original Message-----
> From: Steve Dekorte [[hidden email]]
> Sent: Wednesday, November 15, 2000 3:17 PM
> To: Multiple recipients of list
> Subject: Re: Data inheritance problem
> 
> 
> 
> Pete Gardner wrote:
> >counter:child_inc()	-- but this works on counter_base's 
> data, not counter's...
> > Am I overlooking something?  Is this expected behaviour?  
> How can I make 
> > child_inc() work on it's own data? 
> 
> If you want it to work on the child you need to change:
>     function counter:child_inc() self.parent:inc() end
> to:
>     function counter:child_inc() self.parent.inc(self) end
> or more simply:
>     function counter:child_inc() self:inc() end
> 
> If you implement a method lookup tag on your objects.
> 
> Steve
>