C++从 Java 角度来看:我一定错过了一些东西

发布于 2024-09-16 16:15:01 字数 1015 浏览 3 评论 0原文

首先声明一下,由于本人知识有限,以下想法纯属个人观点。我无意说 C++ 不酷。

我从事 C++ 编程已经有一年了,我认为它确实有一些很酷的功能。尽管如此,我还是感到有点空虚和失望,因为从一个恰好学过 Java(作为第一语言)的人的角度来看,我并没有真正从 C++ 中学到任何“改变思维”的东西。

根据我读过的许多帖子,人们更喜欢 C++,因为它更快。对于像我这样以前没有编写过时间关键型应用程序的程序员来说,我还没有机会欣赏这一点。

到目前为止,我所学到的似乎都是关于语法的。这是我们用 Java 编写类的方法,下面是用 C++ 编写类的方法。这是在 Java 中如何进行继承,在 C++ 中也是如此,等等。 (我知道,多重继承很酷,但对我来说,这不是一件改变主意的事情。我认为很酷的是能够回答为什么Java不/不能支持多重继承,这应该是更通用的比单一继承)。

不知何故,对我来说,所有这些都只是语法,到目前为止,我的思维在编写 C++ 代码后似乎还没有增长。我认为我的问题是用“Java 思维”编写 C++ 程序。正如许多人建议的那样,我真正想要的是在学习一门新语言后改变我的思维方式。我还没有用我的 C++ 做到这一点。

我还可以编写一些小型 Python 程序。然而,我害怕了解更多,因为对我来说,这只是学习一种新的语法,一种做不同事情的新方法,而不知道原因。

我打算学习C以真正了解事物。我认为这将是一种非常“涉及”的语言。

让我知道您的想法并请给我一些建议。

PS:顺便说一句,我想确认一下 C++ 中的一个具体问题。在 C++ 中,如果我是正确的,按以下方式编写效率不高:

    private A computeAndReturnA(){...} 

相反,将其编写为:

private void computeAndReturnA(A& a){...}

与第一种方式一样,返回的值被复制(当我们分配 b = 计算...时)并引入一些低效率? (在Java中,我想第一种方法的含义很明确,而且效率也不错,因为它通过引用传递东西)

Before anything, let me first clarify that the below thoughts are purely my personal opinions and due to my limited knowledge. I have no intention whatsoever to say that C++ is not cool.

I've been programming C++ for like a year and I think it really has some cool features. Nevertheless, I feel a bit empty and disappointed as I didn't really learn any "mind-changing" things from C++, from the standpoint of a person who happens to have previously learned Java (as the 1st language).

According to the many posts I've read, people prefer C++ as it's faster. To a programmer like me who haven't programmed time-critical applications before, I've yet to have a chance to appreciate this.

So far, what I've learned seems to me are all about syntaxes. This is how we write a class in Java, and here's how to write it in C++. This is how to do inheritance in Java and that's how to do in C++ and so on. (I know, multiple inheritance is cool, but to me, not a mind-changing thing. I think what's cool is to be able to answer why Java didn't/couldn't support multiple inheritance, which is supposed to be more general than single inheritance).

Somehow to me, all are just syntaxes and my mind hasn't seemed to grow after coding C++, so far. I think my problem it to write C++ programs with a "Java-mind". What I really want is, as many persons suggest, to change my ways of thinking after learning a new language. I've yet to get to that with my C++.

I can also write a couple of small Python programs. However, I feel afraid to learn more about it as to me, again, it would be just learning a new syntax, a new way of doing things that are just different, without knowing the reasons.

I plan to learn C to really get to know things. I think it would be a quite "involving" language.

Let me know what you think and please give me some advice.

PS: Btw, there is one specific question in C++ I want to confirm. In C++, writing in the following way is not efficient, if I'm correct:

    private A computeAndReturnA(){...} 

Instead, write it as:

private void computeAndReturnA(A& a){...}

as in the first way, the returned values are copied (when we assign b = compute...) and introduce some inefficiencies? (In Java, I guess the 1st way is clear in meaning and okay in efficiency as it passes things by reference)

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

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

发布评论

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

评论(8

不弃不离 2024-09-23 16:15:01

简而言之,你完全错了。事实上,与 Java 相比,C++ 提供了巨大的自由度。

例如,您可以在堆栈上分配类。 Java 不提供这一点。您可以在编译时计算某些值。模板比泛型提供更多的功能。你有能力让某些东西成为参考或价值。在 Java 中,所有这些选择都被剥夺了。就像,在 C++ 中,您可以扩展多个类。您不必被迫扩展对象。如果您愿意,可以进行确定性资源清理。我可以继续说下去。

如果您所做的只是学习语法变体,那么完全有可能以某种可接受的方式使用 C++。然而,C++ 提供了许多在 Java 中看不到的东西。

我猜,简单的事实是,Java 更像是 C++ 的子集,具有更大的标准库,加上反射和运行时代码生成。

我更喜欢 C++,因为坦白说,Java 充满了任意的限制。你知道为什么Java中没有“friend”语句吗?因为詹姆斯·高斯林认为这违背了他的原则。伟大的。太棒了。现在我需要将我的实现分成两个类,并且必须假装这是两个单独封装的实现,因为他十年前认为这不是正确的做法。这是我个人的例子,说明了为什么 Java 非常糟糕 - 你按照 James Gosling 所说的方式编程,而不是你想要的方式,或者在很多情况下,你实际应该的方式。

另外,我看了你的PS。这就是 C++ 有合适的编译器的原因。现实情况是,几乎所有编译器都会为您将第一种形式转换为第二种形式,并且您不想知道的其他一些令人费解的优化是在幕后完成的。

You're totally wrong, in short. The fact is that C++ offers a HUGE quantity of freedom compared to Java.

For example, you can allocate classes on the stack. Java doesn't offer that. You can compute certain values at compile-time. Templates offer far more power than generics. You have the power to make something a reference, or a value. In Java all of these choices are taken away from you. Like, in C++ you can extend more than one class. You aren't forced to extend Object. Deterministic resource clean up, if you want to. I could go on and on and on and on.

If all you do is learn the syntactic variants, then it's perfectly possible to use C++ in a somewhat acceptable fashion. However, C++ offers dozens of things that you would never see in Java.

The simple truth is that Java is more like a subset of C++ with a bigger standard library, plus reflection and run-time code generation, I guess.

I prefer C++ because Java, frankly, is full of arbitrary restrictions. Do you know why there's no "friend" statement in Java? Because James Gosling thought it went against his principles. Great. That's awesome. Now I need to split my implementation up into two classes and have to pretend that it's two implementations with separate encapsulation it because he thought ten years ago that it wasn't the right thing to do. That's my personal example of why Java sucks tremendously - you program how James Gosling says you should, not how you want to or, in plenty of cases, how you actually should.

Also, I looked at your PS. That's why C++ has a proper compiler. The reality is that virtually all compilers will turn the first form into the second for you, and some other mind-bending optimizations that you don't want to know about that are done behind the scenes.

╰ゝ天使的微笑 2024-09-23 16:15:01

学习 C 是您的最佳选择 - 它会让您了解最基本的知识,这迫使您使用 Java 思维方式,因此您可以轻松、轻松地过渡到 C++。

Learning C is your best option here - It'll get you down to the bare bones, which forces to not use your Java mindset and as such you can have a nice, easier transition to C++.

屋檐 2024-09-23 16:15:01

简单的事实是,学​​习用语言思考(例如最佳实践)需要相当长的时间。这是一个漫长的过程,您从学习语法开始,可能一开始就应用其他语言范例来找到自己的方法。

确实,如果您选择了一种与 Java 非常不同的语言(在语法和范式方面),您可能会更早地注意到这些差异,但不要低估 C++ 作为一种不同的范式。

C++ 是一种学习起来非常复杂的语言,但是它给人一种很容易学习的印象,特别是因为它的语法与其他几种语言相似,但它的功能远不止于此。例如,一个大主题是模板编程,使用模板相当简单,但是如果您看一下 boost 模板库,您会发现它可能非常复杂,并且构建这样的库并不适合胆小的人。

学习一门语言不仅仅是学习语法,而是学习如何用该语言思考。

也就是说,C++ 是一种允许您随心所欲地自由和控制的语言,有时甚至是太多的自由。

The simple truth is that it takes quite some time to learn to think in a language e.g. best practices. It's a long process where you start with learning the syntax and probably in the beginning apply other language paradigms at first in order to find your way.

It's true that if you would have picked a language that is very different from Java - syntax-wise as well as paradigm-wuse you may noticed these differences earlier but don't underestimate C++ as a different paradigm.

C++ is a language that is very complicated to learn, however it's a language that can give an impression that it is easy to learn especially since the syntax is similar to several other languages however there is more to it than that. One big topic is for instance template programming, using a template is fairly straightforward however if you take a look at the boost template library you will that it can be quite complex and building such a library is not for the faint hearted.

Learning a language is just not about learning a syntax, its about learning how to think in that language.

That said, C++ is one of languages that allows you as much freedom and control as you want, sometimes too much freedom.

笑梦风尘 2024-09-23 16:15:01

一定要看看 www.boost.org,这将是一次改变思维方式的体验。

至于Post Scriptum中的问题:第一种形式是否涉及复制:这取决于,并且取决于许多因素。

在当前的C++中:

首先,如果编译器支持RVO,特别是当它还支持NRVO时,很可能不会发生高拷贝。然后取决于函数的编写方式:

A computeAndReturnA()
{
   return A( x, y );  // no copy almost for sure
}

A computeAndReturnA()
{
   A a;
   a.f();
   a.g();
   return a;  // no copy if compiler implements NRVO
}

A computeAndReturnA()
{
   A a;
   while ( condition1 ) {
      a.f();
      if ( condition2 )
         return A();  // copy will take place
   }
   a.g();
   return a;  // copy will take place
}

然后取决于函数的调用方式:

A a1 = computeAndReturnA();   // no copy if function body written appropriately,
                              // return value will be constructed directly in a1
A a2;
a2.foo();
a2 = computeAndReturnA();     // copy regardless of function body,
                              // return value can't directly be constructed in a2
                              // as a2 is already constructed

在即将推出的 C++ 中(2011 年 3 月?):

与当前 C++ 的推理相同。但是,如果该推理表明将进行复制,并且类(此处为 A)具有移动构造函数和移动赋值运算符,则将发生“移动”而不是“复制”。在理想的情况下,移动将被省略,并且您将获得直接构造,但如果没有,您将在最坏的情况下移动,当然前提是返回的类是移动感知的。

我想您会发现这篇文章有趣且内容丰富:
http://cpp-next.com/archive /2009/08/想要按值速度传递/

Definitely have a look at www.boost.org, that will be a mindset-changing experience.

As for the question in Post Scriptum: if the first form will involve copy or not: it depends, and depends on many factors.

In current C++:

The first is that if the compiler supports RVO and especially when it also supports NRVO, chances are high copy will not take place. Then it depends on how the function is written:

A computeAndReturnA()
{
   return A( x, y );  // no copy almost for sure
}

A computeAndReturnA()
{
   A a;
   a.f();
   a.g();
   return a;  // no copy if compiler implements NRVO
}

A computeAndReturnA()
{
   A a;
   while ( condition1 ) {
      a.f();
      if ( condition2 )
         return A();  // copy will take place
   }
   a.g();
   return a;  // copy will take place
}

Then it depends on how you call the function:

A a1 = computeAndReturnA();   // no copy if function body written appropriately,
                              // return value will be constructed directly in a1
A a2;
a2.foo();
a2 = computeAndReturnA();     // copy regardless of function body,
                              // return value can't directly be constructed in a2
                              // as a2 is already constructed

In upcoming C++ (March 2011?):

The same reasoning as for current C++ applies. However, if that reasoning suggests that copy would be made, and if the class (A here) has move constructor and move assignment operator, then 'move' and not 'copy' will take place. In the ideal cases the move will be elided as well as you get direct construction, but if not you'll get move in the worst case, of course provided that the class being returned is move-aware.

I think you'll find this article interesting and informative:
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

合久必婚 2024-09-23 16:15:01

C++ 比 Java 更通用,正如其他答案中指出的那样,它是一种多范式语言,而 Java 只强调面向对象编程(以及执行 OOP 的单一方法)。

当然,您可以在 C++ 中完成许多在 Java 中做不到的事情。然而,对于具有 Java 背景的人来说,我认为 C++ 是一种非常糟糕的学习工具,因为在 Java 中简单地编写代码并到处添加一些“删除”语句太容易了。

如果你想要一些改变思维的体验,你应该学习一种积极强调另一种范式的语言,而不是一种让你选择你想要的任何范式的语言。

我建议使用 Haskell(函数式编程)、Erlang(一种不常见的并发方法)、Factor(基于堆栈的编程)、Prolog(逻辑编程)、Common Lisp(CLOS 和宏)、Javascript(基于原型的 OOP、即没有课程)。

在我看来,这些将是改变思想的学习经历。我认为,尽管 Python 和 Ruby 确实支持 Java 不支持的许多功能,但它们仍然非常接近 Java 模型;事实上,如果有 Java 背景,很容易将它们视为同一模型上的另一个皮肤,语法略有不同,并且没有类型声明。

我的观点是,如果你想让自己大吃一惊,你就必须学习一种新的范式。虽然在 C++ 等足够强大的语言上学习新范式当然是可能的,但通过学习迫使您使用该范式的语言来学习新范式会容易得多。

要简要概述一些改变思维的想法,您可以观看这些视频讲座

C++ is much more general than Java, in the sense that it is, as pointed in other answers, a multi-paradigm language, where as Java only stresses object-oriented programming (and a single way to do OOP, at that).

You can certainly do many things in C++ that you couldn't dream of in Java. However, for someone who has a Java background, I'd argue that C++ is a very bad learning vehicle, as it is way too easy to simply code in Java with a few "delete" statements here and there.

If you want some mind-changing experience, you should learn a language that actively stresses another paradigm, instead of a language that lets you choose whatever paradigm you want.

I would suggest one of Haskell (functional programming), Erlang (an uncommon approach to concurrency), Factor (stack-based programming), Prolog (logic programming), Common Lisp (CLOS & macros), Javascript (prototype-based OOP, i.e. without classes).

These would, in my opinion, be mind-changing learning experiences. I would argue that, even though they do support many things that Java doesn't, Python and Ruby are still pretty close to the Java model; indeed, with a Java background, it would be quite easy to see them as just another skin on the same model, with a slightly different syntax and no type declarations.

My point is, if you want to blow your mind, you have to learn a new paradigm. And while it is certainly possible to learn new paradigms on a sufficiently powerful language such as C++, it will be much easier to learn the new paradigm through learning a language that forces you to use that paradigm.

For a brief overview of some mind-changing ideas, you could watch these video lectures.

可可 2024-09-23 16:15:01

如果您正在尝试学习新语言来提高编程技能,也许可以尝试学习一些与 C 风格语法完全不同的东西。也许是 Haskell、Scala 或 Erlang 等函数式语言之一。这将向您展示编程语言有多么不同,您也会开始意识到 Java/C++/C 之间的细微差别。

否则,如果您的目标是解决特定问题,您可以为此选择一种语言。除非你有一些对性能至关重要的东西,否则坚持使用垃圾收集语言可能会更容易。除非你打算进行 iPhone 开发,在这种情况下你应该切换到 Objective-C

If you are trying to learn new languages to improve your programming skill, perhaps try and learn something that is quite different from the C style syntax. Perhaps one of the functional languages like Haskell, Scala or Erlang. That will show you how different programming languages can be and you'll start to realize the subtle differences between Java/C++/C as well.

Otherwise if you are aiming to solve a specific problem you can choose a language for that. Unless you have something thing is performance critical it is probably easier to stick to garbage collected languages. Unless you're planning on doing iPhone development, in which case you should switch to Objective-C

一片旧的回忆 2024-09-23 16:15:01

关于您的问题:即将推出的 C++0x 将具有移动语义,这应该将“按值返回”速度加快到与“按引用返回”相当的性能。这些不需要更改客户端代码,但可能需要在移动的类中实现额外的构造函数。

关于您的咆哮:阅读 Andrei Alexandrescu 所著的《现代 C++ 设计:通用编程和设计模式应用》,您会惊讶地发现 C++ 和 Java(以及相应的思维方式)有如此大的差异。

On your Question: The upcoming C++0x will have move semantics, which should speed up "return by value" to comparable performance of "return by reference". These dont need a change in client code, but one may need to implement an additional constructor in the moved classes.

About your Rant: Read "Modern C++ Design: Generic Programming and Design Patterns Applied" by Andrei Alexandrescu and by surprised how much C++ and Java (and the corresponding mindset) can differ.

泪之魂 2024-09-23 16:15:01

您可能不觉得 C++ 给您带来任何好处的原因是您来自 Java 背景,而 Java 背景确实提供了非常直接和简单的 C++ 转换。可以肯定的是,这不是最好的 C++,但可能是一个可行的 C++ 实现......

... PS 示例中不同习惯用法的原因与引用无关(Java 没有:Java 有指针 -对象,但没有引用;但 C++ 确实有引用和指针),这与 Java 内存分配相对便宜而 C++ 则不然(因为在 Java 中,内存分配的成本分摊到所有内存分配成本)有关。无论如何,VM 都需要分配——而在 C++ 中则不然——所以在 Java 中,VM 确保只根据需要分配内存)。

因此,在高效的 C++ 中,您会看到人们在紧密循环中避免了大量冗余的 new 调用,而在 Java 中,人们可以轻松地创建他们想要的临时对象。

The reason why you probably don't feel like C++ offers you any kind of candy for your mind is that you come from a Java background which to all intents and purposes offers a very direct and simple translation into C++. Not the best C++ to be sure, but a possible working C++ implementation...

... The reason for the different idiom in your PS example has nothing to do with references (which Java doesn't have: Java has pointers-to-objects but not references; but C++ does have references as well as pointers) and everything to do with the fact that in Java memory allocation is relatively cheap whereas in C++ it is not (because in Java the cost of memory allocation is amortized over everything the VM needs to allocate anyway -whereas in C++ it is not- so in Java the VM ensures that memory is only allocated as needed anyway).

So in efficient C++ you see that people avoid lot's of redundant new calls in a tight loop whereas in Java people can comfortably create temporary objects all they want.

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