如果我对 c++ 的理解请纠正我是错误的

发布于 2024-11-04 07:35:08 字数 369 浏览 0 评论 0原文

如果我目前对 C++ 的以下理解有误,请纠正我:

  1. C++ 是 C 的扩展版本。因此,C++ 与 C 一样高效。
  2. 此外,任何用 C 编写的应用程序都可以使用 C++ 编译器进行编译
  3. C 语法也是有效的C++ 语法
  4. C++ 与 C 处于完全相同的语言级别层次结构。

语言级别层次结构

例如。最底层:汇编语言, 高级:Java、PHP 等,

所以我的解释是

C++/C 比 Java、PHP 等处于较低级别,因为它更接近硬件级别(因此,它比 Java、PHP 等更高效) ,但它不像汇编语言那么极端....但C++/C彼此处于同一级别,并且都不接近硬件级别

correct me if any of my following current understanding of c++ is wrong:

  1. C++ is an extended version of C. Therefore, C++ is just as efficient as C.
  2. Moreover, any application written in C can be compiled using C++ compilers
  3. C syntax is also valid C++ syntax
  4. C++ is at the exact same language level hierarchy as C.

Language Level Hierarchy

eg. lowest-level: assembly language,
high-levels: Java, PHP, etc

so my interpretation is that

C++/C is at a lower level than Java,PHP etc since it's closer to hardware level (and therefore because of this,it's more efficient than Java, PHP, etc), yet it is not as extreme as assembly language....but C++/C is at the same level with each other and neither one is closer to hardware level

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我纯我任性 2024-11-11 07:35:08
  1. 如果您从与 C 和 C++ 都合法的代码开始,它通常会编译为两者相同的结果,或者足够接近,以至于效率仅受到最小程度的影响。

  2. 可以编写 C++ 不允许的 C 语言(例如,使用名称与 C++ 中添加的关键字之一相同的变量,例如 new)。然而,大多数此类情况转换起来都很简单,因此 C++ 中允许它们。最难转换的情况可能是使用函数声明而不是原型的代码(或者使用根本没有声明的函数,这在旧版本的 C 中是允许的)。

  3. 参见 2 - 某些 C 语法不能像 C++ 那样工作。如前所述,转换通常很简单。

  4. 不,不是真的。尽管 C++确实提供了与 C 相同的低级操作,但它也提供了 C 所缺乏的高级操作。

  1. If you start with code that's legal as both C and C++, it will typically compile to the same result with both, or close enough that efficiency is only minimally affected.

  2. It's possible to write C that isn't allowable as C++ (e.g., using a variable with a name that's the same as one of the key words added in C++, such as new). Most such cases, however, are trivial to convert so they're allowed in C++. Probably the most difficult case to convert is code that uses function declarations instead of prototypes (or uses functions without declarations at all, which was allowed in older versions of C).

  3. See 2 -- some syntactical C won't work as C++. As noted, it's usually trivial to convert though.

  4. No, not really. Although C++ does provide the same low-level operations as C, it also has higher-level operations that C lacks.

昵称有卵用 2024-11-11 07:35:08

C++ 与 C 处于完全相同的语言级别层次结构。
语言级别层次结构
例如。最低层:汇编语言,高层:Java、PHP等

编程语言通常分为第一代(机器代码)、第二代(汇编语言)、第三代(命令式语言)、第四代(定义有点模糊) - 用于高生产力的领域特定语言,例如 SQL),第五代(问题表达的典型语言,例如数学符号、逻辑或人类语言;Miranda、Prolog)。参见例如 http://en.wikipedia.org/wiki/Fifth- Generation_programming_language 及其链接。

从这个意义上说,C 和 C++ 都是第三代语言。 (正如 Jerry 指出的,PHP、Java、PERL、Ruby、C#...也是如此)。按照这个标准,这些语言属于同一组……它们都是你必须告诉计算机如何解决问题的语言,但不是在特定于 CPU 的级别上。

但从另一种意义上说,C++ 具有比 C 更高级别的编程概念,例如面向对象、函子以及更多多态功能(包括模板和重载),尽管它们都是组织和访问解决问题的步骤的方法。高级语言(即 5GL)不需要被告知 - 相反,他们只需要问题的描述并知道如何解决整个问题领域,他们会为您的具体情况找到可行的方法。

C++/C 的级别比 Java、PHP 等低,因为它更接近硬件级别(因此,它比 Java、PHP 等更高效),但它不像汇编语言那么极端。 ...但是 C++/C 彼此处于同一级别,并且都不接近硬件级别

这有点令人困惑。总结:

让我们看几个例子:

  • 位移位:Java 被设计为比 C 或 C++ 更可移植(有时以牺牲性能为代价),因此即使使用 JIT,某些操作在某些平台上也可能有点低效,但它它们的操作可预测性可能会很方便。如果您正在做同等的工作,并且关心 CPU 行为不同的边缘情况,您会发现 C 和 C++ 将运算符行为留给实现来指定。您可能需要为不同的部署平台编写多个版本的代码,只是为了最终获得与 Java 几乎相同的性能(但程序通常知道它们不会执行边缘情况,或者不这样做)不关心行为差异)。在这方面,Java 已经抽象出了低级问题,可以合理地被认为是更高级别但悲观的。

  • C++ 提供了一些更高级别的功能,例如模板(以及模板元编程)和多重继承。编译器通常提供低级设施,例如内联汇编以及从其他对象/库调用任意函数的能力,只要函数签名在编译时已知即可(某些库解决了此限制)。基于解释型语言(例如 PHP)和基于虚拟机的语言(例如 Java)在这方面往往表现较差。

  • Java 还提供了一些 C++ 所缺乏的高级设施 - 例如内省、序列化。

一般来说,我倾向于认为 C++ 的范围比 Java 低和高。换句话说,Java 与 C++ 的跨度中间的一部分重叠。但是,Java 也有一些突出的高级功能。

PHP 是一种解释性语言,它再次抽象了一些低级问题,但通常也无法为更抽象或更健壮的编程技术提供良好的设施。与大多数解释器一样,它确实允许对任意源代码进行运行时评估,以及类元数据等的运行时修改,这允许采用高级、强大但危险的非结构化方法进行编程。这种事情在编译语言中是不可能的,除非编译器是在部署环境中提供的(即使这样也有更多的限制)。

C++ 是 C 的扩展版本。因此,C++ 与 C 一样高效。

通常是这样。

此外,任何用 C 编写的应用程序都可以使用 C++ 编译器进行编译
C 语法也是有效的 C++ 语法

存在一些细微的差异,例如:

  • 在 C++ 中,main() 必须具有返回类型 int,并且如果不返回语句,则在退出时隐式返回 0遇到过,但 C 允许 voidint 并且后者必须显式返回 int
  • C++ 有额外的关键字(例如 mutable code>、virtualclassexplicit...)因此不是合法的 C++ 标识符,但在 C 中是合法

的本质上是正确的。

C++ is at the exact same language level hierarchy as C.
Language Level Hierarchy
eg. lowest-level: assembly language, high-levels: Java, PHP, etc

Programming languages are often categorised from 1st generation (machine code), 2nd generation (assembly language), 3rd generation (imperative languages), 4th generation (definition's a bit vague - domain-specific languages intended for high productivity, e.g. SQL), 5th generation (typical language of the problem expression, e.g. maths notation, logic, or a human language; Miranda, Prolog). See e.g. http://en.wikipedia.org/wiki/Fifth-generation_programming_language and its links.

In that sense, C and C++ are both 3rd generation languages. (As Jerry points out, so are PHP, Java, PERL, Ruby, C#...). Using that yardstick, these languages belong in the same general group... they're all languages in which you have to tell the computer how to solve the problem, but not at a CPU-specific level.

In another sense though, C++ has higher level programming concepts than C, such as Object Orientation, functors, and more polymorphic features including templates and overloading, even though they're all ways to organise and access the steps for solving the problem. Higher level languages (i.e. 5GL) don't need to be told that - rather, they just need a description of the problem and knowing how to solve the entire domain of problems they find a workable approach for your specific case.

C++/C is at a lower level than Java,PHP etc since it's closer to hardware level (and therefore because of this,it's more efficient than Java, PHP, etc), yet it is not as extreme as assembly language....but C++/C is at the same level with each other and neither one is closer to hardware level

This is confusing things a bit. Summarily:

  • C++ and C do span lower than Java/PHP, yes.
  • C++ and C do tend to be more efficient, yes. You can get a general impression of this at http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.html - don't take it too literally, it depends a lot on your problem space.
  • C++ and C both go as low as each other, but C++ has some higher level programming support too (though it's still a 3GL like C).

Let's look at a few examples:

  • bit shifting: Java is designed to be more portable (sometimes at the expense of performance) than C or C++, so even with JIT certain operations might be a bit inefficient on some platforms, but it may be convenient that they operate predictably. If you're doing equivalent work, and care about the edge cases where CPU behaviours differ, you'll find C and C++ leave operator behaviour for the implementation to specify. You may need to write multiple versions of the code for the different deployment platforms, only to end up getting pretty much the same performance as Java (but programs often know they won't exercise edge cases, or don't care about the behavioural differences). In that respect, Java has abstracted away a low-level concern and could reasonably be considered higher level but pessimistic.

  • C++ provides some higher level facilities such as templates (and hence template metaprogramming), and multiple inheritance. Compilers commonly provide low level facilities such as inline assembly and the ability to call arbitrary functions from other objects/libraries as long as the function signatures are known at compile time (some libraries work around this limitation). Interpreted (e.g. PHP) and Virtual Machine based (e.g. Java) languages tend to be worse at this.

  • Java also provides some higher level facilities that C++ lacks - e.g. introspection, serialisation.

Generally, I tend to conceive of C++ spanning both lower and higher than Java. Put another way, Java overlaps a section in the middle of C++'s span. But, Java has a few stand-out high-level features too.

PHP is an interpreted language that again abstracts away some low level concerns, but generally fails to provide good facilities for more abstract or robust programming techniques too. Like most interpreters, it does allow run-time evaluation of arbitrary source code, as well as run-time modification of class metadata etc., which allows a high level, powerful but dangerously unstructured approach to programming. That kind of thing isn't possible in a compiled language unless the compiler is shipped in the deployment environment (and even then there are more limitations).

C++ is an extended version of C. Therefore, C++ is just as efficient as C.

Generally true.

Moreover, any application written in C can be compiled using C++ compilers
C syntax is also valid C++ syntax

There are some trivial differences, e.g.:

  • in C++, main() must have return type int and implicitly returns 0 on exit if not return statement's encountered, but C allows void or int and for the latter must explicitly return an int
  • C++ has additional keywords (e.g. mutable, virtual, class, explicit...) that are therefore not legal C++ identifiers, but are legal in C

Still, your conception is essentially true.

还给你自由 2024-11-11 07:35:08

1/4 和 2/3 似乎说的是非常相似的事情,但是:

  1. 是的(取决于你所说的“扩展”的意思,但在广泛的层面上,是的)
  2. 并不总是
  3. 不总是
  4. 是的

1/4 and 2/3 seem to be saying very similar things, but:

  1. Yes (Depends on what you mean by "extended", but at a broad level, yes)
  2. Not always
  3. Not always
  4. Yes
财迷小姐 2024-11-11 07:35:08

此外,任何用 C 编写的应用程序
可以使用C++编译器进行编译

并非每个 C 程序都可以使用 C++ 编译器进行编译。 C 和 C++ 之间存在一些差异(例如关键字),这些差异在某些方面阻止了 C 和 C++ 的混合。 Stroustrup 在C 和 C++:兄弟姐妹中阐述了一些重要观点。

C++ 是 C 的扩展版本。
因此,C++ 和 C++ 一样高效
C.

这取决于您使用的语言功能。我听说使用 OOP 可能会比使用更像 C 的方法带来更多的缓存未命中。我无法判断这是否属实,因为我没有阅读更多有关该主题的内容。但这可能是应该考虑的事情。这只是性能不易比较的一个例子。

Moreover, any application written in C
can be compiled using C++ compilers

Not every C program can be compiled using a C++ compiler. There are some differences between C and C++ (keywords, for example), that prevent mixing C and C++ in some ways. Stroustrup adresses some important points in C and C++: Siblings.

C++ is an extended version of C.
Therefore, C++ is just as efficient as
C.

That depends on the language features you use. I heard that using OOP might bring more cache misses than using a more C-like approach. I can't tell wether this is true or not, as I didn't read more on that subject. But it might be something which should be considered. This is only one example were performance isn't easy comparable.

羁〃客ぐ 2024-11-11 07:35:08
  1. 这并不完全正确,除了速度较慢的额外 C++ 语言功能之外,还可以进行不同的优化来改变这一点。然而,由于更好的 C++ 类型系统,这些实际上通常对 C++ 有利。

  2. 不,一个重要的例子是 C++ 不支持从 void* 自动转换,例如

    char* c = malloc(10); // 是有效的 C,但不是 C++

    char* c = (char*)malloc(10) //C++ 中必需

  3. 除了 C99 和较新的 C 功能之外,我认为几乎总是如此。请记住,这只是考虑到语法,这并不意味着可以在 C 中编译的所有内容也可以在 C++ 中编译。

  4. 您能详细说明一下您的意思吗?“语言级别层次结构”是什么意思?

  1. This isn't exactly true, beyond extra C++ language features that are slower, there are different optimizations that can be done that will change this. Due to the better C++ type system, these are actually normally in C++'s favor however.

  2. No, a big case is that C++ doesn't support automatic cast from void* so for instance

    char* c = malloc(10); // Is valid C, but not C++

    char* c = (char*)malloc(10) //Is required in C++

  3. Except for C99 and newer C features, I think this is nearly always the case. Keep in mind this is only taking into account syntax this doesn't mean that everything that can compile in C can also compile in C++.

  4. Could you elaborate on what you mean by this, what do you mean by "language level hierarchy"?

眼睛会笑 2024-11-11 07:35:08

摘要:

  1. 确实如此。
  2. 危险的虚假。
  3. 错误的。
  4. 主观

2/3 的一些示例:

  • sizeof 'a' 在 C++ 中为 1,在 C 中为 sizeof(int)
  • char *s = malloc(len+1) ); 是正确的 C,但无效的 C++。
  • char s[2*strlen(name)+1]; 是有效的(尽管危险)C,但无效的 C++。
  • sizeof (1?"hello":"goodbye") 在 C 中是 sizeof(char *)`,但在 C++ 中是 6。

尝试将现有 C 代码编译为 C++ 是完全无效的,即使您找到并“修复”所有编译时错误,也可能会产生危险的错误。编写在两种语言中都有效的代码可能是多语言竞赛的合理入口,但不适用于任何严肃的用途。 C 和 C++ 的交集实际上是一种非常丑陋的语言,是两全其美的。

Summary:

  1. True.
  2. Dangerously false.
  3. False.
  4. Subjective

Some examples for 2/3:

  • sizeof 'a' is 1 in C++ and sizeof(int) in C.
  • char *s = malloc(len+1); is correct C but invalid C++.
  • char s[2*strlen(name)+1]; is valid (albeit dangerous) C, but invalid C++.
  • sizeof (1?"hello":"goodbye") issizeof(char *)` in C but 6 in C++.

Attempting to compile existing C code as C++ is simply invalid and likely to produce dangerous bugs even if you hunt down and "fix" all the compile-time errors. And writing code that's valid in both languages is perhaps a reasonable entry for a polyglot competition, but not for any serious use. The intersection of C and C++ is actually a very ugly language that's the worst of both worlds.

北渚 2024-11-11 07:35:08

您的某些观点的理解是错误的:

1)您的第一点是正确的。C++是c的扩展。

2)第二点是正确的。 C 可以使用 c++ 编译器进行编译。

3) C 的一些语法与 C++ 不同。在c++中,使用结构体,c应该指定结构体名称,但c++不强制指定结构体名称。而且C++有c中没有的类的概念。 C++也有更高的安全机制。

4)C是过程语言,而C++是面向对象的方法。所以 c++ 与 c 并不处于完全相同的语言级别层次结构。

Your understanding is wrong in some of your points:

1) your first point is right.C++ is an extension of c.

2) second point is right . C can be compiled using c++ compilers.

3) Some of C syntax varies from c++. In c++, using structure , c should specify structure name but c++ it is not mandatory to specify structure name.Also C++ have the concept of class that is not available in c. C++ also have higher security mechanisms.

4)C is procedural language but c++ is object oriented approach. so c++ is not at the exact same language level hierarchy as c.

赠佳期 2024-11-11 07:35:08
  1. C 语言不是 C++ 语言的子集。例如,检查 C99 规范 - 它不会轻易在 C++ 编译器中编译。然而,大多数 C89 源代码可以复制并粘贴到 C++ 源代码。
    与裸机相比,C 和 C++ 是可以“零开销”实现的语言。

  2. 没有。但大多数 C++ 编译器也是 C 编译器。这意味着您可以使用相同的工具链编译 .C 和 .C++ 文件。

  3. 不,这些语言的演变是不同的。请参阅问题 1 的答案。

  4. C++ 是多范式语言。是的,它可以像 C 一样使用。但它也可以用作 DSL——它提供了更高的抽象级别。

  1. C language is not a subset of C++ lanaguage. Check the C99 spec for example - it will not compile in C++ compiler easily. However most of C89 source code can be copied&paste to C++ source code.
    C and C++ are languages that can be implemented with "zero overhead" comparing to bare iron.

  2. No. But most of C++ compilers are C compilers too. It means that you can compile .C and .C++ files using the same toolchain.

  3. No, The evolution of these languages differs. See answer to question 1.

  4. C++ is multiparadigm language. Yes, it can be used in the same way as C. But it can be used as DSL too - it provides greater abstraction level.

乞讨 2024-11-11 07:35:08

这是一个需要回答的大问题。

  1. 并非在所有情况下!
  2. 不正确,因为 3 个
  3. 不正确
  4. 它们并不完全相同

我认为语言级别的层次结构对于事物来说并不是太重要。例如,C 与汇编语言相比是高级语言,而与 Java/C# 相比是低级语言。

That's a whole big question to answer.

  1. Not in all cases!
  2. not true because of 3
  3. not true
  4. They are not exactly the same

I don't think language level hierarchy matters too much for a thing. For example, C is a high-level one compared to assembly language while it's a low-level one compared with Java/C#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文