任何C代码都是有效的C ++代码? [英] Any C code are valid C++ code?
问题描述
由于C是C ++的子集,所以任何C代码或C库(printf(),
scanf()等...)
都有效C ++代码。那是对的吗?所以尽管整个程序
是用
C编写的,但是扩展名为.cpp,我们仍然认为是C ++程序?
请指教。谢谢
Since C is a subset of C++, so any C code or C libraries (printf(),
scanf(), etc...)
are valid C++ code. Is that correct? so even though the whole program
is written in
C, but with .cpp extension, we still consider as C++ program?
Please advise. Thanks
推荐答案
jr * *******@hotmail.com 写道:
由于C是C ++的一个子集[...]
Since C is a subset of C++ [...]
错误的前提。错误的结论。你的subj的答案是不。
Wrong premise. Wrong conclusion. The answer to your subj is "no".
一般来说答案是不。比如一个基本的区别
(即使你创建一个简单的hello world程序并用C和C ++编译器在
下编译它)在C和C ++之间几乎依赖于
名称。机制。 C ++使用扩展的装饰方法给链接器有关名称解析的指示
。
tecnique与C声明命名不兼容。
这就是为什么你必须指定externC的原因。 {...}围绕C
代码以确保
兼容性。
显然还有其他一些东西可以使这两种语言非常远/>
甚至如此相似! ;)
Gianguglielmo
The answer is ''no'' in general.For example one of the basic difference
(even if you create a simple hello world program and compile it under
with a C and a C++ compiler) between C and C++ relies almost in the
name mangling mechanism. C++ uses an extended decoration method to give
the linker indications about the name resolutions.
That tecnique is not compatible with C declaration naming.
This is the reason why you have to specify extern "C" {... } around C
code to ensure
compatibility.
Obviously there are other things that makes the two language very far
even so similar! ;)
Gianguglielmo
jr ******** @ hotmail.com 写道:
由于C是C ++的子集
Since C is a subset of C++
C不是C ++的子集。 C ++与C有一些不兼容的变化。
然而,它们足够兼容,两者都运行了很多代码。
一些不兼容性包括:
*不同的链接机制(这是可以解决的,带有外部C)
*对多维数组的不同解释
*许多C程序包括typedef并定义哪个覆盖C ++
关键字,因此在C ++中不允许(typedef int bool; for
example)
Jon
----
学习使用Linux汇编语言进行编程
http://www.cafeshops.com/bartlettpublish.8640017
这篇关于任何C代码都是有效的C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!