Skip to content

Is it possible to create a LUA subclass form an exported C++ class? #24

Answered by gengyong
NI-LAm asked this question in Q&A
Discussion options

You must be logged in to vote

in luaaa, exported class is userdata instead of table:

print("type of AwesomeCat:", type(AwesomeCat.new("CCC")))

output:

type of AwesomeCat:   userdata

we can wrapper userdata to table to make inheritance works, likes below:

SpecialCat = {}

function SpecialCat:new(name)
	o = AwesomeCat:new(name)
	setmetatable(self, getmetatable(o))
	return self
end

but there is an issue in luaaa codes, luaaa always expect an userdata from stack, so we had to store origin userdata in table, and read it from luaaa by specified name, we use '@' as it's name:

function SpecialCat:new(name)
	o = AwesomeCat:new(name)
	setmetatable(self, getmetatable(o))
	self['@'] = o
	return self
end

now wen can extend Specia…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by gengyong
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request good first issue Good for newcomers
2 participants
Converted from issue

This discussion was converted from issue #15 on June 22, 2022 00:01.