简体   繁体   English

如果我对 c++ 的理解有误,请纠正我

[英]correct me if my understanding of c++ is wrong

correct me if any of my following current understanding of c++ is wrong:如果我目前对 c++ 的以下任何理解有误,请纠正我:

  1. C++ is an extended version of C. C++ 是 C 的扩展版本。 Therefore, C++ is just as efficient as C.因此,C++ 与 C 一样高效。
  2. Moreover, any application written in C can be compiled using C++ compilers此外,任何用 C 编写的应用程序都可以使用 C++ 编译器进行编译
  3. C syntax is also valid C++ syntax C 语法也是有效的 C++ 语法
  4. C++ is at the exact same language level hierarchy as C. C++ 与 C 处于完全相同的语言层次结构。

Language Level Hierarchy语言层次结构

eg.例如。 lowest-level: assembly language, high-levels: Java, PHP, etc最低层:汇编语言,高层:Java、PHP等

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 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....但是 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.如果您从 C 和 C++ 合法的代码开始,它通常会编译成相同的结果,或者足够接近,效率只会受到最小的影响。

  2. It's possible to write C that isn't allowable as C++ (eg, using a variable with a name that's the same as one of the key words added in C++, such as new ).可以将不允许的 C 写成 C++(例如,使用名称与 ZF6F87C9FDCF8B3C3F07F93F1EE8712 中添加的关键字之一相同的变量,例如new 9712C)。 Most such cases, however, are trivial to convert so they're allowed in C++.然而,大多数这样的情况是微不足道的,因此它们在 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).可能最难转换的情况是使用 function 声明而不是原型的代码(或使用完全没有声明的函数,这在旧版本的 C 中是允许的)。

  3. See 2 -- some syntactical C won't work as C++.见 2 --一些语法 C 不能作为 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.尽管 C++确实提供了与 C 相同的低级操作,但它也具有 C 所缺乏的高级操作。

1/4 and 2/3 seem to be saying very similar things, but: 1/4 和 2/3 似乎在说非常相似的事情,但是:

  1. Yes (Depends on what you mean by "extended", but at a broad level, yes)是(取决于您所说的“扩展”是什么意思,但在广义上,是的)
  2. Not always不总是
  3. Not always不总是
  4. Yes是的

Moreover, any application written in C can be compiled using C++ compilers此外,任何用 C 编写的应用程序都可以使用 C++ 编译器进行编译

Not every C program can be compiled using a C++ compiler.并非每个 C 程序都可以使用 C++ 编译器进行编译。 There are some differences between C and C++ (keywords, for example), that prevent mixing C and C++ in some ways. 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 . Stroustrup 在C 和 C++: Siblings中提到了一些重要的点。

C++ is an extended version of C. C++ 是 C 的扩展版本。 Therefore, C++ is just as efficient as C.因此,C++ 与 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.我听说使用 OOP 可能会比使用更像 C 的方法带来更多的缓存未命中。 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.这只是一个例子,性能不容易比较。

C++ is at the exact same language level hierarchy as C. C++ 与 C 处于完全相同的语言层次结构。 Language Level Hierarchy eg.语言层次结构,例如。 lowest-level: assembly language, high-levels: Java, PHP, etc最低层:汇编语言,高层:Java、PHP等

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, eg SQL), 5th generation (typical language of the problem expression, eg maths notation, logic, or a human language; Miranda, Prolog).编程语言通常分为第 1 代(机器代码)、第 2 代(汇编语言)、第 3 代(命令式语言)、第 4 代(定义有点模糊 - 旨在提高生产力的特定领域语言,例如 SQL)、第 5 代(问题表达的典型语言,例如数学符号、逻辑或人类语言;Miranda、Prolog)。 See eg http://en.wikipedia.org/wiki/Fifth-generation_programming_language and its links.参见例如http://en.wikipedia.org/wiki/Fifth-generation_programming_language及其链接。

In that sense, C and C++ are both 3rd generation languages.从这个意义上说,C 和 C++都是第三代语言。 (As Jerry points out, so are PHP, Java, PERL, Ruby, C#...). (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.使用该标准,这些语言属于同一个通用组......它们都是您必须告诉计算机如何解决问题的语言,但不是在特定于 CPU 的级别上。

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.不过,在另一种意义上,C++ 比 C 具有更高级别的编程概念,例如 Object Higher level languages (ie 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.不需要告诉更高级别的语言(即 5GL) - 相反,他们只需要对问题的描述并知道如何解决整个问题域,他们会为您的特定情况找到可行的方法。

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 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....但是 C++/C 彼此处于同一级别,并且没有一个更接近硬件级别

This is confusing things a bit.这有点令人困惑。 Summarily:总结:

  • C++ and C do span lower than Java/PHP, yes. C++ 和 C 的跨度确实低于 Java/PHP,是的。
  • C++ and C do tend to be more efficient, yes. C++ 和 C 确实往往更有效,是的。 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. 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). 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.位移:Java 被设计为比 C 或 C++ 更便于移植(有时会以牺牲性能为代价),因此即使使用 JIT,在某些平台上运行某些操作可能也会很方便。 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.如果您正在做相同的工作,并且关心 CPU 行为不同的边缘情况,您会发现 C 和 C++ 将操作符行为留给实现指定。 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).可能需要为不同的部署平台编写多个版本的代码,最终获得与 Java 几乎相同的性能(但程序通常知道它们不会执行极端情况,或者不关心行为差异)。 In that respect, Java has abstracted away a low-level concern and could reasonably be considered higher level but pessimistic.在这方面,Java 已经抽象出一个低层次的关注点,可以合理地认为是更高层次的但悲观的。

  • C++ provides some higher level facilities such as templates (and hence template metaprogramming), and multiple inheritance. C++ 提供了一些更高级别的工具,例如模板(以及模板元编程)和多个 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).只要 function 签名在编译时已知(一些库绕过此限制),编译器通常提供低级工具,例如内联汇编和从其他对象/库调用任意函数的能力。 Interpreted (eg PHP) and Virtual Machine based (eg Java) languages tend to be worse at this.解释型(例如 PHP)和基于虚拟机(例如 Java)的语言在这方面往往更差。

  • Java also provides some higher level facilities that C++ lacks - eg introspection, serialisation. Java 还提供了一些 C++ 缺乏的更高级别的设施 - 例如内省,序列化。

Generally, I tend to conceive of C++ spanning both lower and higher than Java.一般来说,我倾向于设想 C++ 的范围既低于又高于 Java。 Put another way, Java overlaps a section in the middle of C++'s span.换句话说,Java 与 C++ 跨度中间的一个部分重叠。 But, Java has a few stand-out high-level features too.但是,Java 也有一些突出的高级特性。

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. PHP 是一种解释性语言,它再次抽象出一些低级关注点,但通常也无法为更抽象或更健壮的编程技术提供良好的设施。 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.像大多数解释器一样,它确实允许对任意源代码进行运行时评估,以及对 class 元数据等进行运行时修改,从而实现高级、强大但危险的非结构化编程方法。 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. C++ 是 C 的扩展版本。 Therefore, C++ is just as efficient as C.因此,C++ 与 C 一样高效。

Generally true.一般是真的。

Moreover, any application written in C can be compiled using C++ compilers C syntax is also valid C++ syntax此外,任何用 C 编写的应用程序都可以使用 C++ 编译器 C 语法也是有效的 ZF6F877C9FDCF8EEB3C2F 语法

There are some trivial differences, eg:有一些细微的区别,例如:

  • 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++ 中, main()必须具有返回类型int ,如果没有遇到 return 语句,则在退出时隐式返回 0,但 C 允许voidint并且后者必须显式返回一个int
  • C++ has additional keywords (eg mutable , virtual , class , explicit ...) that are therefore not legal C++ identifiers, but are legal in C C++ has additional keywords (eg mutable , virtual , class , explicit ...) that are therefore not legal C++ identifiers, but are legal in C

Still, your conception is essentially true.不过,您的概念基本上是正确的。

  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.这并不完全正确,除了速度较慢的额外 C++ 语言功能之外,可以进行不同的优化来改变这一点。 Due to the better C++ type system, these are actually normally in C++'s favor however.由于更好的 C++ 类型系统,这些实际上通常对 C++ 有利。

  2. No, a big case is that C++ doesn't support automatic cast from void* so for instance不,一个大案例是 C++ 不支持从 void* 自动转换,例如

    char* c = malloc(10);字符* c = malloc(10); // Is valid C, but not C++ // 是有效的 C,但不是 C++

    char* c = (char*)malloc(10) //Is required in C++ char* c = (char*)malloc(10) //在C++中需要

  3. Except for C99 and newer C features, I think this is nearly always the case.除了 C99 和更新的 C 功能外,我认为几乎总是如此。 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++.请记住,这仅考虑语法,这并不意味着可以在 C 中编译的所有内容也可以在 C++ 中编译。

  4. Could you elaborate on what you mean by this, what do you mean by "language level hierarchy"?您能否详细说明您的意思,“语言级别层次结构”是什么意思?

Summary:概括:

  1. True.真的。
  2. Dangerously false.危险的虚假。
  3. False.错误的。
  4. Subjective主观

Some examples for 2/3: 2/3的一些例子:

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

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.尝试将现有的 C 代码编译为 C++ 是完全无效的,并且即使您查找并“修复”所有编译时错误也可能产生危险的错误。 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. C 和 C++ 的交集实际上是一种非常丑陋的语言,是两全其美的语言。

That's a whole big question to answer.这是一个很大的问题要回答。

  1. Not in all cases!并非在所有情况下!
  2. not true because of 3不正确,因为 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#.例如,C 与汇编语言相比是高级语言,而与 Java/C# 相比是低级语言。

Your understanding is wrong in some of your points:您的某些观点的理解是错误的:

1) your first point is right.C++ is an extension of c. 1)你的第一点是对的。C++是c的扩展。

2) second point is right. 2)第二点是对的。 C can be compiled using c++ compilers. C 可以使用 c++ 编译器进行编译。

3) Some of C syntax varies from c++. 3) 一些 C 语法与 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. 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. C++也有更高的安全机制。

4)C is procedural language but c++ is object oriented approach. 4)C 是过程语言,但 c++ 是面向 object 的方法。 so c++ is not at the exact same language level hierarchy as c.所以 c++ 与 c 的语言层次结构不同。

  1. C language is not a subset of C++ lanaguage. C 语言不是 C++ 语言的子集。 Check the C99 spec for example - it will not compile in C++ compiler easily.例如,检查 C99 规范 - 它不会在 C++ 编译器中轻松编译。 However most of C89 source code can be copied&paste to C++ source code.但是大部分 C89 源代码可以复制并粘贴到 C++ 源代码中。 C and C++ are languages that can be implemented with "zero overhead" comparing to bare iron.与裸机相比,C 和 C++ 是可以以“零开销”实现的语言。

  2. No. But most of C++ compilers are C compilers too.不,但大多数 C++ 编译器也是 C 编译器。 It means that you can compile.C and.C++ files using the same toolchain.这意味着您可以使用相同的工具链编译.C 和.C++ 文件。

  3. No, The evolution of these languages differs.不,这些语言的演变是不同的。 See answer to question 1.见问题 1 的答案。

  4. C++ is multiparadigm language. C++ 是多范式语言。 Yes, it can be used in the same way as C.是的,可以和 C 一样使用。 But it can be used as DSL too - it provides greater abstraction level.但它也可以用作 DSL——它提供了更高的抽象级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM