lua-users home
lua-l archive

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



> Does ToLua support operator overloading in any way? I'm 
> trying to do the
> following but I'm getting an error saying that there is no 
> support for operator=.
> 
> hbVector3D& operator=  (const hbVertex3D& v);
> const hbVector3D& operator+= (const hbVector3D& v);
> 
> If it isn't currently supported, is it possible to change the 
> code for ToLua to
> support it? Has anyone done this? Or is it just not possible 
> given the dsign of
> ToLua/Lua? This seems like such an important, usable feature 
> that I'm surprised
> it's not supported.
> 
> Thanks again
> Mike

from:
	http://www.tecgraf.puc-rio.br/~celes/tolua/

Overloaded operators
--------------------

tolua automatically binds the following binary operators: 

operator+   operator-   operator*   operator/ 
operator<   operator>   operator<=  operator>=

For the relational operators, toLua also automatically converts a
returned 0 value into nil, so false in C becomes false in Lua. 
As an example, suppose that in the code above, instead of having: 

   Point add (Point& other);              // add points, returning
another one
we had: 
   Point operator+ (Point& other);        // add points, returning
another one

In that case, in Lua, we could simply write: 
	p3 = p1 + p2
The indexing operator (operator[]) when receiving a numeric parameter
can also be exported to Lua. In this case, tolua accepts reference as
returned value, even for basic types. Then if a reference is returned,
from Lua, the programmer can either get or set the value. If the
returned value is not a reference, the programmer can only get the
value. An example may clarify: suppose we have a vector class and bind
the following operator: 
   double& operator[] (int index);
In this case, in Lua, we would be able to write: value = myVector[i] and
also myVector[i] = value, what updates the C++ object. However, if the
bound operator was: 
   double operator[] (int index);
we would only be able to write: value = myVector[i]. 

Free functions (i.e., not class members) that overload operators are not
supported.