Re: possible to overload the array-index operators for a c++ class?
On Mar 25, 2006, at 18:18, anders langlands wrote:
> Is it possible to overload, the array index operator for a c++
> class that's bound to lua?
> I want to do
> --- IN C++ ---
> class MyArray
> // ...
> -- IN LUA---
> a = MyArray()
> a[ 1 ] = 1.1
> print( "a = " .. a )
> I tried adding __newindex and __index to my class_ definition, but
> they're not getting called (altough lua will happily do the example
> above, it's just not filtering through to the c++ side)
No it isn't. In theory it would be possible to overload it for
integers, but not for general types (as C++ allows). The reason is
that in lua the following statements are equivalent:
a['foo'] = bar;
a.foo = bar;
i.e. Any invocation of the -operator would be interpreted as either
accessing an attribute/property or a member function of the object.