简体   繁体   English

getch()和_getch()之间的区别

[英]Difference between getch() and _getch()

What is the difference between the two function that are defined in the conio.h header file- conio.h头文件中定义的两个函数之间有什么区别 -

getch() and _getch(). getch()和_getch()。

Is there a difference in the decalaration? decalaration有区别吗? Or is it simply a difference due to updated standards? 或者仅仅因为更新的标准而有所不同?

It is part of a decision by Microsoft a couple of years ago to interpret the C++ standard more rigidly. 这是几年前微软决定更严格地解释C ++标准的一部分。 It says that all names in the global namespace which start with an underscore are reserved for use by the implementation . 它表示全局命名空间中以下划线开头的所有名称都保留供实现使用 This means that getch is not a reserved name, but _getch is. 这意味着getch不是保留名称,但_getch是。

So Microsoft figured that "this function, and every other POSIX function, is kind of supplied by the implementation. Let's rename them by prepending an underscore, so we're able to keep it inside the "reserved" part of the global namespace. That way, there's no chance of name clashes with user-supplied functions. 所以微软认为“这个函数,以及所有其他POSIX函数,都是由实现提供的。让我们通过预先设置下划线来重命名它们,这样我们就可以将它保留在全局命名空间的”保留“部分内。方式,用户提供的功能没有名称冲突的可能性。

You could say that these are good intentions, or that it's just an evil attempt at breaking POSIX code. 你可以说这些都是好意,或者说这只是打破POSIX代码的邪恶尝试。 I don't know what their true motivation was, but the end result is that according to Microsoft, getch is deprecated, and you should use _getch instead. 我不知道他们的真正动机是什么,但最终的结果是,根据微软的说法, getch已被弃用,你应该使用_getch代替。

Both of these functions are non-standard (ie they are not in C++ ISO standard). 这两个函数都是非标准的(即它们不符合C ++ ISO标准)。 They are extensions provided by your particular toolchain, and as such, you have to check for differences in its documentation. 它们是您的特定工具链提供的扩展,因此,您必须检查其文档中的差异。

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

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