编译C vs C ++ [英] Compiled C vs C++

查看:59
本文介绍了编译C vs C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我最近做了一些Java,我想要使用一些

C ++而不是C,只是因为类而已。语法看起来更好

现在方便。


让我想知道的一件事是C ++代码大小。我听说过

C ++应该只是一个加强的C,让你按需付费,

而且我不打算去任何地方。但是我写的一个小玩具类

比同等的C结构生成了更多的代码,在测试主()中实例化了




C代码完成了它应该做的:malloc,填充一些struct

字段并返回。 C ++代码生成了两个相同的(!)构造函数

函数,它们完成了C函数的功能。通话代码 -

课程! - 只调用其中一个,因为在语法级别上只有一个构造函数

。另外,生成了某种结构

(我假设,班级),即使我没有任何虚拟的东西,而且我没有b
不要看看我打算写的代码哪里需要访问

到类结构。


所以我的问题是:为什么有两个构造函数当我只用
写了一个(即使在最终的,链接的可执行文件中)?什么/为什么代码中有趣的

结构? (编译器是GCC / G ++ 4.0,但我确定3.x不是/ b $ b大不相同/更好)


我是确定这是常见问题解答(虽然谷歌没有提出任何确实有用的答案),所以请随时指导我过去的帖子。


谢谢,

Ulrich

Hi, since I''ve done a bit of Java recently, I''m thinking of using some
C++ instead of C, just because the "class" syntax is looking more
convenient now.

One thing that makes me wonder is C++ code size though. I''ve heard that
C++ is supposed to be just a souped up C that makes you "pay as you go",
and I don''t intend to go anywhere far. But a small toy class I wrote
generated much more code than the equivalent C struct, when instantiated
in a test main().

The C code did just what it was supposed to: malloc, fill some struct
fields and return. The C++ code generated two identical (!) constructor
functions that did about what the C one did. The calling code - of
course! - only called one of them, because on the syntax level there was
only one constructor. In addition, some kind of structure was generated
(I assume, the class), even though I don''t have anything virtual, and I
don''t see where code like I''m planning to write would ever need access
to the class structure.

So my question would be: why are there two constructors when I only
wrote one (even in the final, linked executable)? What/why is the funny
structure in the code? (compiler is GCC/G++ 4.0, but I''m sure 3.x isn''t
much different/better)

I''m sure this is something of a FAQ (though Google didn''t turn up any
really useful answer), so feel free to direct me to past posts.

Thanks,
Ulrich

推荐答案

Ulrich Hobelmann写道:


[...]


....但我写的一个小玩具类
Ulrich Hobelmann wrote:

[...]

.... But a small toy class I wrote
生成了比等效的C结构更多的代码,当实例化时/>在一个测试主()中。
generated much more code than the equivalent C struct, when instantiated
in a test main().




这可能有助于查看你编写的C ++代码和C代码,

包括类等效的C struct和main()函数。它可能还有

帮助查看选项(如果有的话),你传递给编译器。


问候

Andy Little



It would probably help to see the C++ code and C code you wrote,
including class equivalent C struct and main ()function. It might also
help to see the options (if any), you passed to the compiler.

regards
Andy Little


Ulrich Hobelmann写道:
Ulrich Hobelmann wrote:
因为我最近做了一点Java,我在想使用一些
C ++而不是C,只是因为类而已。语法现在看起来更方便。

让我想知道的一件事是C ++代码大小。我听说过C ++应该只是一个加强的C,它会让你按需付费,
而且我不打算去任何地方。


*你*的意思是随时付费?

但是我写的一个小玩具类产生了更多的代码在测试main()中实例化时,比等效的C结构。

C代码完成了它应该做的:malloc,填充一些struct
字段并返回。 C ++代码生成了两个相同的(!)构造函数
函数,它们完成了C函数的功能。调用代码 -
当然! - 只调用其中一个,因为在语法级别上只有一个构造函数。另外,生成了某种结构(我假设,班级),即使我没有任何虚拟的东西,我也看不到像我这样的代码。计划编写将需要访问类结构。


你怎么知道它产生了两个相同的构造函数?因为你看了生成的汇编?
?如果它确实产生了一个你不使用的函数(这似乎更像是你的
编译器中的错误或效率低下而不是定义的语言问题)在C ++

标准中),自20世纪70年代以来的任何链接器都将从最终的

可执行文件中删除它们。同样,你在哪里绘制了关于编译器生成的

神秘结构的信息?

所以我的问题是:为什么我只有两个构造函数? >写了一个(即使在最终的链接可执行文件中)?什么/为什么代码中有趣的
结构? (编译器是GCC / G ++ 4.0,但我确定3.x并不是很不一样/更好)

我确定这是常见问题解答(虽然谷歌没有找到任何真正有用的答案),所以请随时指导我过去的帖子。
Hi, since I''ve done a bit of Java recently, I''m thinking of using some
C++ instead of C, just because the "class" syntax is looking more
convenient now.

One thing that makes me wonder is C++ code size though. I''ve heard that
C++ is supposed to be just a souped up C that makes you "pay as you go",
and I don''t intend to go anywhere far.
What do *you* mean by "pay as you go"?
But a small toy class I wrote
generated much more code than the equivalent C struct, when instantiated
in a test main().

The C code did just what it was supposed to: malloc, fill some struct
fields and return. The C++ code generated two identical (!) constructor
functions that did about what the C one did. The calling code - of
course! - only called one of them, because on the syntax level there was
only one constructor. In addition, some kind of structure was generated
(I assume, the class), even though I don''t have anything virtual, and I
don''t see where code like I''m planning to write would ever need access
to the class structure.
How do you know it generated two identical constructors? Because you
looked at the generated assembly? If it did generate a function that
you don''t use (which seems more like a bug or inefficiency in your
compiler rather than a problem with the language as defined in the C++
standard), any linker since the 1970s will strip them from the final
executable. Likewise, where did you draw your information about the
mysterious structure that the compiler generated?
So my question would be: why are there two constructors when I only
wrote one (even in the final, linked executable)? What/why is the funny
structure in the code? (compiler is GCC/G++ 4.0, but I''m sure 3.x isn''t
much different/better)

I''m sure this is something of a FAQ (though Google didn''t turn up any
really useful answer), so feel free to direct me to past posts.




如已经要求,请发帖您可比较的C

程序和C ++程序的代码。这将帮助我们达到这个的底部。


干杯! --M



As already requested, please post your code for both the comparable C
program and the C++ program. That will help us get to the bottom of
this.

Cheers! --M


kwikius写道:
kwikius wrote:
Ulrich Hobelmann写道:
[...]它也可能
帮助查看选项(如果有的话),你传递给了编译器。
Ulrich Hobelmann wrote:
[..] It might also
help to see the options (if any), you passed to the compiler.




不是真的。编译器选项讨论属于新闻组

决定给那些编译器。感谢您保持此论坛的主题。


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问



Not really. Compiler options discussions belong to the newsgroups
decidated to those compilers. Thanks for keeping this forum on topic.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于编译C vs C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆