简体   繁体   English

需要帮助来解释C ++中的功能

[英]Need help for explain a function in C++

I am doing simple version of pacman for c++. 我正在为C ++做简单版本的pacman。 When I look up for some example in the internet, I found some code which I dont really understand. 当我在互联网上查找一些示例时,我发现了一些我不太了解的代码。 Please help me to explain it, grateful for that. 请帮我解释一下,对此深表感谢。

void GoToXY(int column, int line)
{
// Create a COORD structure and fill in its members.
// This specifies the new position of the cursor that we will set.
COORD coord;
coord.X = column;
coord.Y = line;

// Obtain a handle to the console screen buffer.
// (You're just using the standard console, so you can use STD_OUTPUT_HANDLE
// in conjunction with the GetStdHandle() to retrieve the handle.)
// Note that because it is a standard handle, we don't need to close it.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// Finally, call the SetConsoleCursorPosition function.
if (!SetConsoleCursorPosition(hConsole, coord))
{
    // Uh-oh! The function call failed, so you need to handle the error.
    // You can call GetLastError() to get a more specific error code.
    // ...
}
}

My questions are: What does this line do? 我的问题是:这条线是做什么的? : HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); :处理hConsole = GetStdHandle(STD_OUTPUT_HANDLE); Why we have an empty if? 为什么我们有一个空的if? And what is the point of an empty if statement? 空if语句的意义是什么?

The line you ask about gives you a file handler for the standard output. 您询问的行为您提供了标准输出的文件处理程序。 It is likely that below it is used to print something to the console, or may be to draw the actual Pacman game there. 可能是它下面用来在控制台上打印东西,或者可能是在那儿绘制实际的吃豆子游戏。

Here's some documentation on it, that might be helpful: 这是一些文档,可能会有所帮助:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075(v=vs.85).aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/ms682075(v=vs.85).aspx

With regard to your second question, empty if is usually pointless. 关于第二个问题, if通常没有意义, if空。 In your particular case the author of the example just points out, that GetStdHandle can fail (which is highly unlikely in reality), and that you might want to handle it in some way (may be print some error message and gracefully exit), but he himself doesn't fill it in. 在您的特殊情况下,示例的作者仅指出, GetStdHandle可能会失败(实际上极不可能),并且您可能希望以某种方式处理它(可能会打印一些错误消息并正常退出),但是他自己没有填写。

Now as you develop your game on top of that example you might either choose to ignore that error (in which case you might as well just remove that entire if block), or handle it in some way (in which case you would fill in the body of that if statement) 现在,在该示例的顶部开发游戏时,您可以选择忽略该错误(在这种情况下,您也可以删除整个if块),或以某种方式处理该错误(在这种情况下,您可以填写该if语句的主体)

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetStdHandle(STD_OUTPUT_HANDLE) retrieves a HANDLE to the standard output (Which is usually the active console) see here GetStdHandle(STD_OUTPUT_HANDLE)将HANDLE检索到标准输出(通常是活动控制台), 请参见此处

A HANDLE is pointer to anything. HANDLE是指向任何东西的指针。 You can see here 你可以在这里看到

Answer to your second and third question is, it is not necessary here to write an empty if statement. 对第二个和第三个问题的回答是,这里没有必要写一个空的if语句。 Control will go to the if statement, if the SetConsoleCursorPosition() function doesn't work correctly. 如果SetConsoleCursorPosition()函数无法正常运行,则控制将转到if语句。 So, you can handle your error in the if, and if u don't want to handle the error, leave the if statement empty. 因此,您可以在if中处理您的错误,如果您不想处理错误,请将if语句留空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM