C 和 C++ 之间是什么关系? (更多技术方法)
我正在阅读 C 和 C++ 之间的关系,并对更技术性的了解感兴趣的问题。
例如,如果您想在 C++ 中使用线程,您(假设是 Linux 平台)会只使用 pthread 吗?我知道 C++ 有 STL 和 Boost 库填补了很多空白...但是 C++ 程序员通常使用 C 代码库吗?
我正在尝试确定是否值得学习 C++,因为我已经了解 C(诚然,学习 C 不是先决条件,而且可能是一个缺点),但我不确定 C++ 和 C 库等如何结合在一起。 ..
I was reading through Relationship between C and C++ and was interested in a more technical look at the question.
For example, if you want to use threads in C++, would you (assuming a Linux platform) just use pthreads? I know C++ has the STL and the Boost libraries fill in a lot of gaps... but do C++ programmers generally use libraries for C code?
I'm trying to decide if it's worth learning C++ as I already know C (admittedly learning C is not a pre-requisite and can be a disadvantage) but I'm not sure how C++ and C libraries etc all tie in together....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在 C++03 之前,您可能会使用 pthreads。当前的 C++11 草案已将线程内置到标准库中,因此您可能会使用它(尽管它紧密基于 pthreads,因此它们之间几乎没有实际差异)。
不过,一般来说,直接从 C++ 使用 C 库很容易,并且在没有 C++ 库的情况下,通常使用它们。就此而言,即使有 C++ 库,有些人有时还是更喜欢使用 C 库。
Up through C++03, yes, you'd probably use pthreads. The current draft of C++11 has threading built into the standard library, so you'd probably use that instead (though it's based closely on pthreads, so there's little practical difference between them).
In general, however, yes, it's easy to use C libraries directly from C++, and in the absence of a C++ library for the purpose it's common to use them. For that matter, even when there is a C++ library, some people sometimes prefer to use C libraries anyway.
C 库在 C++ 中不断使用。有时它们被封装在更好的惯用 C++ 接口中,有时则不然。例如,有很多围绕 C 套接字 API 的精简 C++ 包装器,但人们经常直接使用套接字。我一直在C++中直接使用各种POSIX函数。
不过,您确实需要确保您已经了解 C(尝试将 C++ 字符串直接传递给需要 C 字符串的 C 函数的新 C++ 程序员的数量......)。并且需要时间来学习做某些事情的最佳方法(需要指向数据数组的指针的 C 函数?只需给它 C++ 向量的第一个元素的地址,不要手动分配/构建新的大批)。
C libraries are used in C++ constantly. Sometimes they get wrapped in a nicer idiomatic C++ interface, sometimes not. Lots of thin C++ wrappers around C socket APIs, for example, but people often use sockets directly anyway. I directly use various POSIX functions in C++ all the time.
You do need to make sure you already understand C, though (the number of new C++ programmers who try to pass C++ strings directly to C functions expecting C-strings...). And it takes time to learn the best ways to do certain things (that C function that needs a pointer to an array of data? Just give it the address of the first element of your C++ vector, don't manually allocate/build a new array).
如果您只关心 C 和 C++ 库的互操作性。
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
是一个非常好的链接。
如果您从未进行过面向对象编程,我建议您尝试一下 C++。两者都是图灵完备的语言,没有什么是 C++ 做不到而 C 已经做不到的。
If you are just concerned with interoperability of C and C++ library.
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B
is a pretty good link.
If you have never done objected oriented programming, I would recommended giving C++ a try. Both are Turing complete language, There is nothing C++ cannot do that C already can't.