Casting in Lua

My question is how I can cast to a derived type in Lua. Here’s an example of what I mean:
class BaseClass


class DerivedClass : public BaseClass
void printTest()

class ObjectManager {
BaseClass getObject( string name )


// for C++, one can cast it like so
DerivedClass d = (DerivedClass)om.getObject(‘objectName’);

now for Lua script:
local x = ObjectManager.getObject(‘myDerivedClassObject’)
x.PrintTest() – crash because getObject returns BaseClass type.

How can I cast x to be of the type ‘DerivedClass’? In particular, a cast that works for all types and wouldn’t just require hard coding in C++ with functions like BaseClassToDerivedClass() as so many variations of this would be required. I can’t find anything on the net about casting of this kind in Lua. Any input is appreciated :slight_smile:

After some googling, I found someone with a similar problem

It seems that Lua doesn’t have any facilities for typing… when I looked at the official Lua documentation, I couldn’t find anything even relating to classes…

What are you trying to do there? Casts are obsolete in Lua because variables all have a dynamic type, which means you can store strings, numbers, tables, functions in a table without the need to cast them.
If you ever need to cast something when programming in Lua, then you did something seriously wrong.

@Databean. Thanks :slight_smile: I had a look at that too but came to the same conclusion as you :frowning:

@Kilburn. I agree with you that it shouldn’t be necessary. I’m new to Lua so probably missing something but I can’t see any other way of doing this with changing the C++ code for the getObject function, which is not something I can do given my particular situation. How would this be done using just tables etc. as you mentioned?


If that is literally your c++ code, then the DerivedClass isn’t even extending the BaseClass…
I’m betting its not, but if it is, then shouldn’t it be
class DerivedClass : public BaseClass {


Oh, sorry. That was just pseudo code. I’ll fix that to avoid confusion. Thanks :slight_smile:


What if I used templates like:
template T, U
T castObject( U object, int type )
switch( type )
return (DerivedClass*)object;

or something to that effect. Would that work, do you think?

How long is your code? Would you mind posting it here to make everything a bit clearer?

There’s a nice little table.Inheric function. It will basically copy all the content from parent to child. You’ll be able to use all the functions from the parent.

Alternatively you can set the metatable with __index being the table to inherit from.

You can set the metatable with ‘setmetatable’, but it won’t allow for multiple metatables.

Oh, these are all great ideas. I’ll get cracking on trying some these. Thanks :smiley:

You could make the __index metamethod check multiple tables to try to find a value. That way, a single metatable can allow more than one base table.

local MyTableA = { 1,2,3 }
local MyTableB = { [4]=4,[5]=5,[6]=6 }

local myMeta = { __index = function( tab, key )
if (MyTableA[ key ]) then return MyTableA[ key ] end
if (MyTableB[ key ]) then return MyTableB[ key ] end
end }

local DaTable = {}
setmetatable( DaTable, myMeta )

print( DaTable[1], DaTable[2], DaTable[3], DaTable[4], DaTable[5], DaTable[6] )[/lua]

Not sure if that will help OP or not.