使用luabind调用从基类继承的C ++成员函数。

C++代码,一个控制台项目

class CControl  //: public CGameObject
{
public:
    CControl(){}
    ~CControl(){}

public:
    void AddAnimation(){ cout << "CControl::AddAnimation" << endl;}
};

int  _tmain()
{
    lua_State* L = lua_open();
    luaL_openlibs(L);
    open(L);

    module(L)
    [
        class_<CControl>("CControl")
            .def(constructor<>())
            .def("AddAnimation",&CControl::AddAnimation)
    ];

    int result = luaL_dofile(L,"scripts/test.lua");
    cout << result << endl;
    return 0;
}

使用luabind的lua代码

class 'Button' (Control)
function Button:__init()
    Control:__init()
end

function Button:Create()
    self:AddAnimation()    --调用,失败了
end

d = Button()
d:Create()

问题:

当我在Button:Create中调用继承的函数self:AddAnimation()时。哇哇哇! "CControl::AddAnimation"并没有打印出来!怎么回事?我已经检查了两个小时了。令人沮丧!任何帮助都将不胜感激。

enter image description here

点赞
用户921626
用户921626

我明白了!!如果你想成功调用 Control 构造函数,你必须像这样编写代码 "Control.(self)"。顺便说一下,当你想要调用成员函数时,你应该编写 " self:AddAnimation"。

2012-07-16 15:26:50