虽然重构了一些遗留的C++代码,但我发现我可能通过某种方式定义一个可以指向共享相同签名的任何类方法的变量来删除一些代码重复.经过一番挖掘,我发现我可以做以下事情:
class MyClass
{
protected:
bool CaseMethod1( int abc, const std::string& str )
{
cout << "case 1:" << str;
return true;
}
bool CaseMethod2( int abc, const std::string& str )
{
cout << "case 2:" << str;
return true;
}
bool CaseMethod3( int abc, const std::string& str )
{
cout << "case 3:" << str;
return true;
}
public:
bool TestSwitch( int num )
{
bool ( MyClass::*CaseMethod )( int, const std::string& );
switch ( num )
{
case …Run Code Online (Sandbox Code Playgroud) 我需要为我的小应用程序监听键盘键状态.
#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
while(1)
{
if(GetKeyState(VK_SPACE) & 0x80)
{
cout << "Space pressed.\r\n";
DoSpaceKeyTask();
}
if(GetKeyState(OTHER_KEY) & 0x80)
{
cout << "Other key pressed.\r\n";
DoOtherKeyTask();
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从键盘上单击某些键后,这些功能必须运行一次.它们只是我的应用程序的一些小任务,这与本主题无关.
我的问题是,当我按下一个键时,由于while(1)在按键期间循环几次,它几次执行这些功能.我不能Sleep()在这种情况下使用,因为它仍然不会有效.
我正在寻找这样的解决方案.
DoSpaceKeyTask() 执行"一次".DoOtherKeyTask() 执行"一次".我喜欢我将要使用的5个键.这个案子有人能帮帮我吗?
PS.如果GetKeyState()功能对此任务无用,请随时向您推荐.我的函数知识在C++上非常有限.