lua-users home
lua-l archive

Newbie: Inheriting with tolua from C++ class

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


Hi there,
i'am trying around with tolua an lua since yesterday, so i'am a 
newbie and maybe you will forgive me if my question is somewhat 
stupid.
i tried to do the following:
i wrote a c++ class foo and "exported" it to lua with the aid of 
tolua. Everithing went just fine.
later, i tried the inheritance example with the Point and ColorPoint 
class from the tolua homepage
which goes like this:
---------------------------------------------------------------------
----------------------------------------------------------------------
- define a Point class 
classPoint = { x=0, y=0 } 
tolua.class(classPoint) -- set as a class 
-- define print method 
function classPoint:print () 
 print(self.x,self.y) 
end 
-- define add method 
function classPoint:add (p2) 
 return Point{x=self.x+p2.x,y=self.y+p2.y} 
end 
-- define a Point constructor 
function Point (p) 
 tolua.instance(p,classPoint) -- set as an instance of classPoint 
return p end 
-- define a Color Point class 
classColorPoint = { color = 'black' } 
tolua.class(classColorPoint,classPoint) -- set as class inheriting 
from classPoint 
-- define class methods 
function classColorPoint:print () 
 print(self.x,self.y,self.color) 
end 
-- define Color Point constructor 
function ColorPoint (p) 
 tolua.instance(p,classColorPoint) -- set as an instance of 
classColorPoint 
 return p 
end 
---------------------------------------------------------------------
---------------------------------------------------------------------
again, everything fine, everyone happy.
now as the next step I changed the line 3 to
tolua.class(classPoint, foo) -- set as a class and inherit from c++ 
class foo
now, that does not semm to work as i expected it. I hoped, that if i 
call
x = ColorPoint{} 
then there would magicaly a new intance of the c++ class foo 
generated... but this semms to be not the case...
on the other hand, using a line like
x = ColorPoint:new()
does the instantiation of an foo-object, but then the type of x is 
not ColorPoint (I think???).
Now the question:
Why is that so?
did i make a mistake?
Is there NO way to inherit from a c++ class exposed by tolua?
Any help appreciated,
thanks and cheers,
David Strippgen

AltStyle によって変換されたページ (->オリジナル) /