使用C头文件而不是C ++中的C ++等价物(例如stdio.h而不是cstdio)是不好的做法吗? [英] Is it bad practice to use a C header instead of its C++ equivalent in C++ (e.g. stdio.h instead of cstdio)?

查看:189
本文介绍了使用C头文件而不是C ++中的C ++等价物(例如stdio.h而不是cstdio)是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来很多人在他们的C ++代码中包含example.h而不是cexample。我知道C ++版本中的一切都在命名空间std中声明,但我不知道任何其他差异。那么为什么人们使用C头文件,可以这样做吗?

解决方案

两者之间的区别是, C ++导入的C头(通过前缀c和删除.h后缀)在命名空间 std 中。这样,任何调用或使用标准设施的前缀都是 std :: ,以实现一致性。这是标准的做事方式(tm )。除非你已经有一堆C代码,你不想在每个标准调用中附加 std :: ,然后使用经典的C头文件。 p>

It seems that a lot of people include example.h instead of cexample in their C++ code. I know that everything in the C++ versions is declared in namespace std, but I'm not aware of any other differences. So why do people use the C headers, and is it okay to do so?

解决方案

The difference between the two is that the C headers that C++ imported (by prefixing with c and removing the .h suffix) are in namespace std. This so any call or use of a standard facility is prefixed with std::, for uniformity. It's The Standard Way Of Doing Things(tm). Unless of course you already have a bunch of C code in which you don't feel like appending std:: to each standard call: then use the classic C headers.

这篇关于使用C头文件而不是C ++中的C ++等价物(例如stdio.h而不是cstdio)是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆