继承并存储静态类信息
我正在尝试用 Lua 设置一些东西,但是 Lua 的细节对于我的问题并不重要。
我希望能够做的是调用一个函数,例如 OpenLib
template <class T>
static void OpenLib(lua_State* L)
{
// this func does some other stuff too that I'm omitting, important bit below
if (T::myTable && T::myTableName)
{
luaL_openlib(L, T::myTableName, T::myTable, 0);
}
}
我尝试了几种不同的方法,但无法让它正常工作。我尝试创建一个包含 myTable 和 myTableName 的基类,如下所示:
class LuaInfo
{
public:
static const char* myTableName;
static luaL_reg* myTable;
}
然后我可以从 LuaInfo 继承,然后填写我需要的信息。这不起作用,因为从 LuaInfo 继承的所有类都会获得相同的信息,所以我环顾四周并得到了这样做的想法:
template <class t>
class LuaInfo
// ...
这使得初始化它的语法有点愚蠢,因为我现在必须执行 class Widget : public LuaInfo,但它更接近工作了。
template <class T>
void OpenLib(lua_State* L)
{
if (T::myTable && T::myTableName)
{
luaL_openlib(L, LuaInfo<T>::myTableName, LuaInfo<T>::myTable, 0);
}
}
我尝试了一些变体来尝试使其正确,但我不断收到错误,例如
undefined reference to `ag::LuaInfo<ag::ui::Widget>::myTable'
我想做的事情是否可能,如果可以,那么正确的方法是什么?
I'm trying to set up some stuff with Lua, but the specifics of Lua aren't important for my question.
What I would like to be able to do is call a function, say OpenLib<T>(L)
, and have it get the table name for a particular class (as well as it's table) and register it with Lua. It essentially boils down to this:
template <class T>
static void OpenLib(lua_State* L)
{
// this func does some other stuff too that I'm omitting, important bit below
if (T::myTable && T::myTableName)
{
luaL_openlib(L, T::myTableName, T::myTable, 0);
}
}
I've tried this a few different ways and I can't get it to work right. I tried making a base class that contains myTable and myTableName like so:
class LuaInfo
{
public:
static const char* myTableName;
static luaL_reg* myTable;
}
Then I could just inherit from LuaInfo, and then fill in the info that I needed. That didn't work because all classes that inherit from LuaInfo would get the same info, so I looked around and got the idea of doing this:
template <class t>
class LuaInfo
// ...
Which made the syntax to initialize it a little silly as I now have to do class Widget : public LuaInfo, but it was closer to working.
template <class T>
void OpenLib(lua_State* L)
{
if (T::myTable && T::myTableName)
{
luaL_openlib(L, LuaInfo<T>::myTableName, LuaInfo<T>::myTable, 0);
}
}
I've tried a few variants of this to try to get it right but I keep getting errors like
undefined reference to `ag::LuaInfo<ag::ui::Widget>::myTable'
Is what I want to do possible, and if so, whats the right way to go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
应该可以正常工作。
您的问题是您需要定义静态变量。
包含一堆这样的行的单个源文件将解决它
等等。
您可能需要定义一个宏来使其更好。
然而,以这种方式扩展有点难看。我认为特征样式模板可能会解决这个问题..但我不确定它们是否可以更好地扩展。
Using
should work OK.
Your problem is that you need to define your static variables.
A single source file containing a bunch of lines like this will solve it
and so on.
You may want to define a macro to make this nicer.
However its a bit ugly to scale that way. I was thinking traits style templates might solve this .. but I'm not sure they scale any better.
你的第一次尝试对我来说效果很好。我猜您只是忘记初始化静态成员并收到一些关于此的链接错误。这就是我所做的:
现在,如果您想向
OpenLib
提供其他信息,您必须创建一个像LuaInfo
这样的新类,并将该新类作为模板参数。但是为什么你想要这个信息作为模板参数呢? IMO,这更简单:
Your first attempt works fine for me. I guess you just forgot to initialize the static members and got some link errors about that. This is what I did:
Now if you want to give other info to
OpenLib
you have to create a new class likeLuaInfo
and give that new class as template parameters.But why do you want this info as a template parameters? IMO, this is much simpler: