possible to overload the array-index operators for a c++ class?

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

possible to overload the array-index operators for a c++ class?

anders langlands
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[1] = " .. a[1] )


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)

cheers

Anders
Reply | Threaded
Open this post in threaded view
|

Re: possible to overload the array-index operators for a c++ class?

Arvid Norberg-2

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[1] = " .. a[1] )
>
>
> 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.

--
Arvid Norberg




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user