On Fri, Oct 24, 2008 at 12:54 PM, klaas.holwerda <[EMAIL PROTECTED]> wrote: > John Labenski wrote: >> I don't know why, didn't it generate something like this? >> >> %operator double operator-( int col, int row ) >> >> double returns = ((*self)-(col, row)); >> >> Anyway, it should be fixed in CVS. >> > I updated, and now get errors. > > http://www.velocityreviews.com/forums/t291623-why-overload-operator-as-friend-.html > > So the friend keyword is needed in C++. And there or two ways to > write/reach the same thing with operator==. > Operators with 2 args, need the friend word to reach the private members. > In principle it is a function which is not part of the class, but > declared inside it. > In any case if you generate the next (after the word WRONG), all is fine. > > %class %noclassinfo %encapsulate a2dAffineMatrix > > %operator bool operator== ( const a2dAffineMatrix& a, const > a2dAffineMatrix& b ) > > Should become: > > static int LUACALL wxLua_a2dAffineMatrix_op_eq(lua_State *L) > { > // const a2dAffineMatrix b > const a2dAffineMatrix * b = (const a2dAffineMatrix > *)wxluaT_getuserdatatype(L, 3, wxluatype_a2dAffineMatrix); > // const a2dAffineMatrix a > const a2dAffineMatrix * a = (const a2dAffineMatrix > *)wxluaT_getuserdatatype(L, 2, wxluatype_a2dAffineMatrix); > // get this > a2dAffineMatrix * self = (a2dAffineMatrix > *)wxluaT_getuserdatatype(L, 1, wxluatype_a2dAffineMatrix); > // call op_eq > // WRONG bool returns = ((*self)(*a, *b)); > bool returns = *a == *b; > // push the result flag > lua_pushboolean(L, returns); > > return 1; > } >
>From the www.velocityreviews.com link you have above, I think the friend is "needed" since you are writing a member operator function with the syntax meant for being outside the class declaration. The compiler you have treats it correctly, but I'm afraid that this exception would be too much to try to handle in genwxbind.lua and it would be better to use the standard syntax. If you look at the generated C++ binding code above, you'll see that three a2dAffineMatrix are retrieved from Lua, but only 2 are needed for the == operator. This is because class member functions expect that the self, the class itself, is always acted on. You have this in your class http://www.wxart2d.org/wxart2dDoxygen/html/afmatrix_8h-source.html friend bool operator== ( const a2dAffineMatrix& a, const a2dAffineMatrix& b ); However if you have the operator function as a member function to the class you only need to write this which is what you did for the *= operator and others: class a2dAffineMatrix { ... bool operator== ( const a2dAffineMatrix& b ); } I think the latter makes more sense since you're writing that the == operator should be applied between the class object instance and "b". I've never understood why operators are often written outside the class. ------------------ There is a short discussion halfway down this page; search for: Complex operator+ (const Complex& a, Complex& b); http://www.acm.org/crossroads/xrds2-1/ovp.html ------------------ -John ps. I am adding the [] operator as op_index() if you need to use that. ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ wxlua-users mailing list wxlua-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/wxlua-users