Re: problems using .def_readwrite and .property when binding classes
"Nigel Atkinson" <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Jon,
This works for me compiled with gcc. It prints 'Hello World' 3 times.
Perhaps you could try this and see what is different.
---------------------8<---------------------------
#include <luabind/luabind.hpp>
#include <iostream>
#include <lua.hpp>
using namespace luabind;
using namespace std;
class Item
{
public:
string name;
Item( string n ) { name = n; }
string getString() { return name; }
void setString( string n ) { name = n; }
};
int main()
{
lua_State* L = lua_open();
luaL_openlibs( L );
luabind::open(L);
module(L)
[
class_<Item>( "Item" )
.def( constructor<string>() )
.def( "getString", &Item::getString )
.def( "setString", &Item::setString )
.def_readwrite( "name1", &Item::name )
.property( "name2", &Item::getString, &Item::setString )
];
const char *script =
"a = Item('testing')\n"
"a:setString('Hello World')\n"
"print( a:getString() )\n"
"print( a.name1 )\n"
"print( a.name2 )\n";
if( luaL_dostring( L, script ) )
cout << lua_tostring( L, -1 ) << endl;
lua_close( L );
return 0;
}
---------------------8<---------------------------
On Tue, February 8, 2011 2:55 pm, Jonathan Capps wrote:
> On Mon, Feb 7, 2011 at 9:32 PM, Nigel Atkinson
> <[email protected]>wrote:
>
>> Hi Jon,
>>
>> The last two print statements in your script should print "Hello World".
>> It looks like the meta-table setting isn't working correctly.
>>
>> Try instead of:
>>
>> a = new Item("testing")
>>
>> Just:
>>
>> a = Item("testing")
>>
>> Nigel
>>
>> Thanks for your response, Nigel. That "new" was a typo from fingers too
> used to C++ -- I couldn't seem to copy/paste from the console window, so I
> had to manually copy what was there. I do in fact use
>
> a = Item("testing")
>
> I've looked over the rest of the code, and it appears to be accurate (but
> my
> eyes are tired). Just to see what would happen, I tried
>
> a = new Item("testing")
> print(a) --prints 'nil', as I expected it
> would
>
> I had wondered if this issue had something to do with a bad or improperly
> set metatable, and I'm glad to have some confirmation, but I'm too much of
> a
> lua newb to know how to diagnose and correct it. Do you have any other
> ideas
> on how to solve this? Google found some other people with the same issue,
> but no solutions, so I was hopeful that someone on the mailing list might
> have seen it before.
>
> Jon
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
> Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb_______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb