简体   繁体   English

在C ++中使用C功能是不好的做法吗?

[英]Is it bad practice to use C features in C++?

例如printf而不是coutscanf而不是cin ,使用#define宏等?

I wouldn't say bad as it will depend on the personal choice. 我不会说坏,因为这将取决于个人选择。 My policy is when there is a type-safe alternatives is available in C++, use them as it will reduce the errors in the code. 我的策略是当C ++中有类型安全的替代品时,使用它们会减少代码中的错误。

It depends on which features. 这取决于哪些功能。 Using define macros in C++ is strongly frowned upon, and for a good reason. 在C ++中使用define宏是非常不受欢迎的,并且有充分的理由。 You can almost always replace a use of a define macro with something more maintainable and safe in C++ (templates, inline functions, etc.) 您几乎总是可以使用C ++(模板,内联函数等)中更易于维护和安全的东西来替换define宏的用法。

Streams, on the other hand, are rightly judged by some people to be very slow and I've seen a lot of valid and high-quality C++ code using C's FILE* with its host of functions instead. 另一方面,Streams被一些人正确判断为非常慢,我已经看到很多有效且高质量的C ++代码使用C的FILE*而不是其主机。

And another thing: with all due respect to the plethora of stream formatting possibilities, for stuff like simple debug printouts, IMHO you just can't beat the succinctness of printf and its format string. 还有一件事:对于过多的流格式化可能性,对于像简单的调试打印输出这样的东西,恕我直言,你只是无法击败printf及其格式字符串的简洁性。

I would say the only ones that are truly harmful to mix are the pairings between malloc / free and new / delete . 我会说唯一真正有害混合的是malloc / freenew / delete之间的配对。

Otherwise it's really a style thing...and while the C is compatible with the C++, why would you want to mix the two languages when C++ has everything you need without falling back? 否则它真的是一种风格的东西......虽然C与C ++兼容,但是当C ++拥有你需要的所有内容而不退缩时,你为什么要混合使用这两种语言呢?

You should definitely use printf in place of cout . 你绝对应该使用printf代替cout The latter does let you make most or all of the formatting controls printf allows, but it does so in a stateful way. 后者确实允许你创建printf允许的大部分或全部格式化控件,但它以有状态的方式完成。 Ie the current formatting mode is stored as part of the (global) object. 即当前格式化模式存储为(全局)对象的一部分。 This means bad code can leave cout in a state where subsequent output gets misformatted unless you reset all the formatting every time you use it. 这意味着错误的代码可能会使cout处于后续输出格式错误的状态,除非您每次使用它时都重置所有格式。 It also wreaks havoc with threaded usage. 它还会对线程使用造成严重破坏。

There are better solutions for most cases, but not all. 对于大多数情况,有更好的解决方案,但不是全部。

For example, people quite often use memcpy . 例如,人们经常使用memcpy I would almost never do that (except in really low-level code). 我几乎不会这样做(除了真正的低级代码)。 I always use std::copy , even on pointers. 我总是使用std::copy ,甚至是指针。

The same counts for the input/output routines. 输入/输出例程的计数相同。 But it's true that sometimes, C-style printf is substantially easier to use than cout (especially in logging). 但有时候,C风格的printfcout更容易使用(特别是在日志记录中)。 If Boost.Format isn't an option then sure, use C. 如果Boost.Format不是一个选项那么肯定,使用C.

#define is a different beast entirely. #define完全是一个不同的野兽。 It's not really a C-only feature, and there are many legitimate uses for it in C++. 它不是真正的C-only功能,在C ++中有许多合法用途。 (But many more that aren't.) (但还有更多不是。)

Of course you'd never use it to define constants (that's what const is for), nor to declare inline functions (use inline and templates!). 当然你永远不会用它来定义常量(这就是const的用途),也不是用来声明内联函数(使用inline和模板!)。

On the other hand, it is often useful to generate debugging assertions and generally as a code generation tool. 另一方面,生成调试断言并且通常作为代码生成工具通常是有用的。 For example, I'm unit-testing class templates and without extensive use of macros, this would be a real pain in the *ss. 例如,我是单元测试类模板而没有广泛使用宏,这将是* ss中真正的痛苦。 Using macros here isn't nice but it saves literally thousands of lines of code. 在这里使用宏并不好,但它可以节省数千行代码。

对于分配,我会完全避免使用malloc / free而只是坚持使用new / delete。

Not really, printf() is quite faster than cout , and the c++ iostream library is quite large. 不是真的, printf()cout快得多,而且c ++ iostream库非常大。 It depends on the user preference or the program itself (is it needed? etc). 这取决于用户偏好或程序本身(是否需要?等)。 Also, scanf() is not suitable to use anymore, I prefer fgets() . 此外, scanf()不再适合使用,我更喜欢fgets()

What can be used or not only depends on the compiler that will be used. 可以使用或不仅仅取决于将使用的编译器。 Since you are programming in c++, in my opinion, to maximize compatibility it is better to use what c++ provides instead of c functions unless you do not have any other choices. 由于您使用c ++编程,在我看来,为了最大化兼容性,最好使用c ++提供的代替c函数,除非您没有任何其他选择。

Coming from a slightly different angle, I'd say it's bad to use scanf in C, never mind C++. 从一个稍微不同的角度来看,我会说在C中使用scanf很糟糕,更别提C ++了。 User input is just far to variable to be parsed reliably with scanf. 用户输入远远不能用scanf来可靠地解析。

I'd just post a comment to another reply, but since I can't... C's printf() is better than C++'s iostream because of internationalization. 我只是发表评论到另一个回复,但因为我不能...因为国际化,C的printf()比C ++的iostream更好。 Want to translate a string and put the embedded number in a different place? 想要翻译字符串并将嵌入的数字放在不同的位置? Can't do it with an ostream. 用ostream做不到。 printf()'s format specification is a whole little language unto itself, interpreted at runtime. printf()的格式规范本身就是一个完整的小语言,在运行时解释。

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

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