C++ 的 C 陷阱和错误程序员
如果您是 C 程序员或熟悉 C 的 C++ 程序员,您能告诉我您从 C++ 程序员中注意到的最常见错误/模式/风格是什么吗?例如,您是否注意到 C 程序员编写的 C 程序与 C++ 程序员编写的 C 程序之间的区别?如果您能提供一个列表,详细说明 C++ 程序员对 C 的主要误解,我将非常感激。
我想学习 C,但是虽然 C++ 背景有所帮助,但我担心它也可能会受到伤害。我有一个奇怪的假设,除了一些关键字和库之外,我不需要学习任何其他东西,因为我了解 C++。我对这种假设感到难过,因为我确实认识到 C++ != C,但有时当我在 C++ 中使用 C 库或维护其他人遗留的过程 C++ 时,差异会变得模糊。
顺便说一句,我不是问 C 中不存在哪些 C++ 功能,或者我们/他们是否使用“malloc”并且他们/我们使用“new”。
谢谢。
If you are C programmer or C++ programmer that knows C well, can you tell me what are the most common mistakes/pattern/style that you noticed from C++ programmers? For example, do you noticed a difference between a C program written by a C programmer vs C program written by C++ programmer? If you can provide a list specifying the major misunderstandings that C++ programmers tend to have about C, I will really appreciate it.
I want to learn C, but while a C++ background helps, I'm afraid that it might hurt as well. I have this weird assumption that besides some keywords and libraries, I don't have to learn anything else because I know C++. I feel bad about having that assumption because I do recognize that C++ != C, but sometimes the difference get blurry when I use C libraries in C++ or maintained legacy procedural C++ from others.
Btw, I'm not asking what are the C++ features not present in C, or whether we/they use "malloc" and they/we use "new".
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我经常看到发生的一件事是正确释放分配的内存。特别是与包含动态分配内存的结构相关。对于 C++,析构函数会自动调用,如果编写正确,它们会负责关联对象的清理。使用 C,您必须记住释放结构体分配的所有内存,或者记住调用某种为您执行此操作的析构函数。
One thing that I see happen quite frequently is properly freeing allocated memory. Especially associated with structures containing dynamically allocated memory. With C++, destructors are automatically called and if properly written they take care of the associated objects clean up. With C you have to remember to either free all the memory allocated with a structure, or remember to call some kind of destruct function that does it for you.
我不确定我是否会将此称为“错误”,但必须使用 C 的经验丰富的 C++ 程序员可能会创建很多看起来像类和虚函数表的东西。
这不一定是坏事,因为您当然可以用 C 语言进行面向对象编程,但对于特定问题来说可能有点过分了。
I'm not sure I'd call this a "mistake", but an experienced C++ programmer who has to use C is likely to create a lot of things that look like classes and virtual-function tables.
This is not necessarily a bad thing, as you can certainly do object-oriented programming in C, but it may be overkill for a particular problem.
我真的不能从个人经验来看,但我相信您可能会遇到一些微妙的问题,例如引用(在变量名前面添加
&
)作为函数参数,如何enum< /code> 不是像 C++ 中那样的完全限定类型,涉及返回 void 指针的内存函数的东西......类似的东西。
I can't really say from personal experience, but I believe you may potentially encounter some subtle problems with things like references (prepending
&
to a variable name) as function parameters, howenum
s aren't fully qualified types like they are in C++, stuff involving memory functions that return void pointers... things like that.几乎,当您找到惯用的 C 代码时,它看起来与惯用的 C++ 代码类似,只不过您必须破解所有缺失的语言功能并实现您自己的半成品版本。例如,宏->模板/内联函数。 void* 指针->遗产。函数指针->函数对象。例外->goto &错误代码。
Pretty much, when you find idiomatic C code, then it looks similar to idiomatic C++ code, except you have to hack around all the missing language features and implement your own half-assed version of them. For example, macros -> templates/inline functions. void* pointers-> inheritance. function pointers->function objects. exceptions->goto & error codes.