Joh*_*ohn 0 c c++ methods struct
如何在C中使用这些方法?我在某处读到它们可以"用结构指针作为第一个参数的函数替换",但我不知道如何做到这一点,如果这是正确的事情.
struct SCustomKeys
{
struct SCustomKey Save[10];
struct SCustomKey Load[10];
struct SCustomKey Slot[10];
struct SCustomKey PrintScreen;
struct SCustomKey LastItem; // dummy, must be last
//--methods--
struct SCustomKey &key(int i) { return ((SCustomKey*)this)[i]; }
struct SCustomKey const &key(int i) const { return ((SCustomKey*)this)[i]; }
};
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它们的示例:
void ZeroCustomKeys (SCustomKeys *keys)
{
int i = 0;
SetLastCustomKey(&keys->LastItem);
while (!IsLastCustomKey(&keys->key(i))) {
keys->key(i).key = 0;
keys->key(i).modifiers = 0;
i++;
};
}
Run Code Online (Sandbox Code Playgroud)
更多背景信息:http://pastebin.com/m649210e8
谢谢您的帮助.尽管如此,我还没有能够建议替换使用此函数的C++方法.关于如何处理这个的任何想法?
void InitCustomKeys (struct SCustomKeys *keys)
{
UINT i = 0;
SetLastCustomKey(&keys->LastItem);
while (!IsLastCustomKey(&keys->key(i))) {
SCustomKey &key = keys->key(i);
key.key = 0;
key.modifiers = 0;
key.handleKeyDown = NULL;
key.handleKeyUp = NULL;
key.page = NUM_HOTKEY_PAGE;
key.param = 0;
i++;
};
//an example key
keys->PrintScreen.handleKeyDown = HK_PrintScreen;
keys->PrintScreen.code = "PrintScreen";
keys->PrintScreen.name = L"Print Screen";
keys->PrintScreen.page = HOTKEY_PAGE_MAIN;
keys->PrintScreen.key = VK_PAUSE;
}
Run Code Online (Sandbox Code Playgroud)
而我现在正在尝试的新功能是:
struct SCustomKey* key(struct SCustomKeys *scs, int i) {
return &(((SCustomKey*)scs)[i]);
}
Run Code Online (Sandbox Code Playgroud)
基本上,而不是像以下成员函数:
struct SCustomKey &key(int i) { return ((SCustomKey*)this)[i]; }
Run Code Online (Sandbox Code Playgroud)
您需要将其重写为一个函数,该函数将指向SCustomKeys的指针作为它的第一个参数.
该功能将如下所示:
SCustomKey* key(SCustomKeys* customKeys, int i)
{ return ((SCustomKey*)(customKeys)+i); }
Run Code Online (Sandbox Code Playgroud)
这应该为您提供指向您尝试访问的元素的指针.