问题描述
我有写作code的习惯是在C和C ++编译器编译,例如:
I have a habit of writing code to be compilable on both C and C++ compilers, for example:
some_type *foo = (some_type *) malloc(sizeof (some_type));
当我问我的C code的问题,有人告诉我不要投malloc的。这是在有保证的C ++编译C分配内存的唯一途径。为什么这是一个在C坏主意?为什么这是一个在C坏主意++?
When I ask questions about my C code, I'm told not to cast malloc. This is the only way to allocate memory in C that's guaranteed to compile in C++. Why is this a bad idea in C? Why is this a bad idea in C++?
的C其他功能我会通过限制自己的那些对C ++的兼容功能的一个子集上缺少哪一个呢?为什么限制自己的同时也是兼容C的特征的一个子集C ++中的坏主意?
Which other features of C am I missing out on by restricting myself to a subset of features that are compatible to C++? Why is restricting myself to a subset of features that are also compatible to C++ a bad idea in C?
的C哪些功能++我会通过限制自己的是到C兼容功能的一个子集,错过了?为什么限制自己的同时也是兼容到C C ++中的坏主意功能的子集?我最感兴趣的是由包括核心语言推出差异,而不是标准库的答案。
Which features of C++ am I missing out on by restricting myself to a subset of features that are compatible to C? Why is restricting myself to a subset of features that are also compatible to C a bad idea in C++? I'm most interested in answers that encompass differences introduced by the core language, rather than the standard libraries.
有没有的更高效的替代纳入C $ C $ç成C ++项目比编译的C code口依靠与相同的C ++编译器我用它来编译我的C ++ code吗?什么时候应该使用C编译器?什么时候应该使用C ++编译器?有什么办法纳入C $ C $ç成C ++项目,即不使用C ++编译器编译C code涉及?这个在你的答案重点将特别有用。
Is there a more productive alternative to incorporate C code into a C++ project than compiling any C code I rely upon with the same C++ compiler I use to compile my C++ code? When should I use a C compiler? When should I use a C++ compiler? Is there any way to incorporate C code into a C++ project, that doesn't involve using the C++ compiler to compile C code? Emphasis on this in your answer would be particularly useful.
推荐答案
您问很多问题:
当我问我的C code的问题,有人告诉我不要投malloc的。这是在有保证的C ++编译C分配内存的唯一途径。为什么这是一个在C坏主意?为什么这是一个在C坏主意++?
铸造的返回值的malloc()
在C被认为是不好的,因为它可以隐藏的事实原型的malloc()
缺失。如果不投,编译器会提醒你,你需要的#include<&stdlib.h中GT;
。演员是在C不必要的,因为语言允许隐式转换无效*
到另一个指针类型。
Casting the return value of malloc()
in C is considered bad because it can hide the fact that the prototype to malloc()
is missing. By not casting, the compiler will alert you that you need to #include <stdlib.h>
. The cast is unnecessary in C, because the language allows for implicit conversion of void *
to another pointer type.
铸件通常是不可取的。在C ++中有许多可用的机制,允许投要避免。例如,新
和删除
运营商,继承,多态和模板的C ++特性,可以利用来写类型安全code,而不需要铸造。
Casting is generally not desirable. In C++ there are many mechanisms available that allow casting to be avoided. For example, the new
and delete
operators, inheritance, polymorphism, and templates are features of C++ that can be leveraged to write type safe code without the need for casting.
的C其他功能我会通过限制自己的那些对C ++的兼容功能的一个子集上缺少哪一个呢?为什么限制自己的同时也是兼容C的特征的一个子集C ++中的坏主意?
我的头,C两种功能,可能是有用的,但在C ++中缺失会(1)聚合类型指定初始化,以及(2)变长数组的顶部。由于C是大多数是C ++的一个子集,我一般不找问题,使用C ++编译器为更强烈的类型安全的C,但你肯定不希望被写code,它是在一堆石膏投掷(类型安全就走出了窗外)。
Off the top of my head, two features of C that might be useful but are missing in C++ would be (1) designated initialization for aggregate types, and (2) variable length arrays. As C is mostly a subset of C++, I don't generally find an issue with using a C++ compiler as a "more strongly type-safe" C, but then you certainly don't want to be writing code that is throwing in a bunch of casts (type-safety goes out the window).
我会用C ++编译器来编译C code中的唯一情况是,如果有遗留的C code大基地,您作为开发商希望更新或功能的修改,这将是在C ++中更容易实现。
The only time I would use a C++ compiler to compile C code is if there was a large base of legacy C code, to which you as the developer wish to update or modify with functionality that would be more easily implemented in C++.
的C哪些功能++我会通过限制自己的是到C兼容功能的一个子集,错过了?为什么限制自己的同时也是兼容到C一个坏主意在C ++?
您上C ++所提供的所有面向对象编程和泛型编程功能被淘汰出局。这样一来,你是不是能够正确地利用语言来充分利用。如果你想编写C code,最好是刚刚与C编译器坚持。
You lose out on all the object oriented and generic programming features that C++ has to offer. As a result, you are not able to properly leverage the language to full advantage. If you wish to write C code, it is better to just stick with a C compiler.
还有没有其他的选择吗?什么时候应该使用C编译器?什么时候应该使用C ++编译器?有什么办法纳入C $ C $ç成C ++项目,即不使用C ++编译器编译C code涉及?
当然有替代品。您使用C code A C编译器和C ++编译器C ++ code。如果C和C ++编译器都来自同一供应商,那么在所有的可能性中,C ABI的两种编译器将是相同的。然后,如果你希望你的C ++ code到能够调用C函数,函数原型为这些功能只需要做广告,他们使用的是C ABI:
Of course there are alternatives. You use a C compiler for C code, and C++ compiler for C++ code. If the C and C++ compiler are from the same vendor, then in all likelihood, the C ABI for both compilers will be the same. Then, if you want your C++ code to be able to call C functions, the function prototypes to those functions just need to advertise that they are using the C ABI:
extern "C" {
/* the C function prototypes of the code compiled by the C compiler */
}
您可以通过使用C ++和C code包含相同的头文件中使用此使用 #IFDEF
来检查,看看是否code是由C ++编译器或不进行编译。
You can use this in the same header file included by both C++ and C code by using a #ifdef
to check to see if the code is being compiled by a C++ compiler or not.
#ifdef __cplusplus
extern "C" {
#endif
/* ...the body of the C header file */
#ifdef __cplusplus
}
#endif
作为最后的警告,除非你有一个强烈的愿望,学习不同的语言,所以一般更富有成效使用您最熟悉的语言。如果有一个C ++编译器使用的要求,那么这将是最好的了解如何利用语言来你最大的利益。
As a final caveat, unless you have a burning desire to learn a different language, it is generally more productive to use the language that you are most familiar with. If there is a requirement that a C++ compiler be used, then it would be best to learn how to leverage the language to your maximum benefit.
这篇关于为什么我不应该编译C code与C ++或写C ++ code为在C编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!