简体   繁体   English

从Java转向c ++

[英]Shift from Java to c++

I have been developing applications based on C# (.net) and Java (J2EE) for the last 3 years. 在过去的3年里,我一直在开发基于C#(。net)和Java(J2EE)的应用程序。

But now I feel, Java, C# makes you lame (from learning point of view) and you can develop your apps quickly but you fail to understand the basic underlying concepts of programming. 但是现在我觉得,Java,C#让你感到茫然(从学习的角度来看),你可以快速开发你的应用程序,但是你无法理解编程的基本概念。

So, I am trying to learn C++, but I find it a little "confusing" due to pointer, multiple inheritance, some conventions and other concepts of C++ which don't exist in Java. 所以,我正在尝试学习C ++,但由于指针,多重继承,一些约定以及Java中不存在的C ++的其他概念,我发现它有点“混乱”。 So, what do you guys suggest? 所以,你们有什么建议? How should I feel about it? 我该怎么想?

Thanks 谢谢

PS: I am a student, so have all the time in the world and actually shift. PS:我是一名学生,所以世界上所有的时间都在转移。

In my opinion, you should learn C first in order to properly understand the base upon which C++ is built. 在我看来,你应该首先学习C,以便正确理解构建C ++的基础。 Pick up a copy of "The C Programming Language" by Kernighan and Ritchie, widely considered the best reference on the language, and start reading through it. 拿起Kernighan和Ritchie的“The C Programming Language”,被广泛认为是该语言的最佳参考,并开始阅读它。 Once you fully understand C, you'll have the low-level base you need. 一旦你完全理解了C,你将拥有所需的低级基础。

C++ is no more "basic and underlying" than any other modern programming language. C ++不再是任何其他现代编程语言的“基础和底层”。 It has a model of a computer (a flat memory address space), but the OS and CPU merely simulates that model using many layers of caching and paging, so it's not "real". 它有一个计算机模型(一个平面内存地址空间),但操作系统和CPU只是模拟使用多层缓存和分页的模型,所以它不是“真实的”。 The result is that the same operation may sometimes take 1000s of times longer to complete than at other times. 结果是,相同的操作有时可能比其他时间长1000倍。

Also modern C++ has lots of powerful abstractions that have no more direct relationship with how a computer works than do the abstractions provided in Java and C#. 现代C ++还有许多强大的抽象,与Java和C#中提供的抽象相比,与计算机的工作方式没有更直接的关系。 The OP mentions multiple inheritance - clearly no more elemental than inheritance in other OO languages. OP提到了多重继承 - 显然没有其他OO语言中的继承元素。 Many other features of C++ omitted from Java are high-level abstractions (or allow you to build them) and so in some ways Java is the more low-level language. 从Java中省略的C ++的许多其他功能是高级抽象(或允许您构建它们),因此在某些方面Java是更低级的语言。 In Java the meaning of operator symbols is always the same, whereas in C++ a simple == might build an object that will later be used to generate a SQL expression instead of being executed in-process. 在Java中,运算符符号的含义始终相同,而在C ++中,简单的==可能会构建一个对象,该对象稍后将用于生成SQL表达式而不是在进程中执行。

The JVM and CLR runtimes are (almost certainly) written in C and/or C++, so in that sense obviously they happen to form layers today. JVM和CLR运行时(几乎可以肯定)用C和/或C ++编写,因此从这个意义上说,它们恰好形成了今天的层。 But at the C/C++ layer you will still be working in an abstraction that is not "how the machine really works", so you'll really just be learning a different set of abstractions, rather than "reality". 但是在C / C ++层,你仍然会在一个不是“机器如何工作”的抽象中工作,所以你真的只是在学习一组不同的抽象,而不是“现实”。 And an OS (or indeed a hardware chip) can be designed specifically so that JVM or CLR like runtimes are the native low-level layer of the system; 并且可以专门设计OS(或实际上是硬件芯片),以便JVM或CLR(如运行时)是系统的本机低级层; on such a system it would be the C/C++ runtime that would require a "high-level" (expensive) emulation layer in order to work. 在这样的系统上,C / C ++运行时需要“高级”(昂贵)仿真层才能工作。

So it is probably not worth trying to learn how to program in "reality". 因此,可能不值得尝试学习如何在“现实”中编程。 No one really does that these days; 这些天没人真的那么做; it's a waste of time. 这是浪费时间。 You're better off learning about how programming abstractions help you to write correct programs. 你最好学习编程抽象如何帮助你编写正确的程序。 If a language makes life difficult for you, that doesn't prove you're doing the "real thing". 如果一种语言让你的生活变得困难,那就不能证明你正在做“真实的事情”。 It just means you picked the wrong language for what you're trying to do. 这只是意味着你选择了错误的语言来完成你想做的事情。

I disagree with the sentiment that you need to learn C or assembly language first. 我不同意你首先需要学习C语言或汇编语言的观点。 C++ and C may be similar in theory but are very different in terms of practical use. C ++和C在理论上可能相似,但在实际使用方面却非常不同。 One gains little to nothing in the way of C++ idioms by using only C, and while it is good to have practical experience in multiple languages, it's an exercise in futility to specify prerequisites in language learning. 通过仅使用C,人们对C ++惯用语几乎没有什么收获,虽然在多种语言中获得实践经验是很好的,但是在语言学习中指定先决条件是徒劳的。 I think the best way to learn the concepts of programming is to sit down with someone who understands them well and just talk about it, be that on StackOverflow, in forums, or, if you're lucky, in person. 我认为学习编程概念的最好方法是与熟悉它们的人坐下来谈谈,在StackOverflow上,在论坛中,或者,如果你幸运的话,亲自参与。

At the end of the day, I think programming really isn't all that hard, and you may need someone to explain it right just one time to have everything click. 在一天结束时,我认为编程确实不是那么难,你可能需要有人一次解释它才能让所有内容都点击。 It's all about rehashing the same simple concepts over and over to build complex and beautiful machines. 这就是为了构建复杂而美观的机器,一遍又一遍地重复相同的简单概念。

For learning c++ I reccommend reading C++ for Java Programmers by Mark Allen Weiss. 为了学习c ++,我推荐阅读Mark Allen Weiss为Java程序员阅读C ++。 It helped me alot when moving from Java to C++ as it is very good at highlighting the differences between the languages. 从Java迁移到C ++时,它帮助了我很多,因为它非常擅长突出语言之间的差异。

But, now I feel, Java, C# makes you lame (from learning point of view) and you can develop your apps quickly but you fail to understand the basic underlying concepts of programming. 但是,现在我觉得,Java,C#让你感到茫然(从学习的角度来看),你可以快速开发你的应用程序,但是你无法理解编程的基本概念。

If you're trying to learn the concepts of programming, rather than machine architecture, there's not much benefit to learning C++. 如果您正在尝试学习编程的概念而不是机器架构,那么学习C ++并没有太大的好处。 I would suggest going with something different from Java all together. 我建议一起使用与Java不同的东西。 A Lisp variant, perhaps. 也许是一个Lisp变种。

How To Design Programs is a pretty good book. 如何设计程序是一本非常好的书。

If you want to understand the underlying concepts of programming languages , I would suggest a book such as John Mitchell's Concepts in Programming Languages. 如果你想了解编程语言的基本概念,我会推荐一本书,如John Mitchell的编程语言概念。 Follow this up by writing a few parsers/interpreters for simple languages. 通过为简单语言编写一些解析器/解释器来跟进此问题。 Another good resources is SICP, which is specific to Scheme (a LISP dialect), and available in full here . 另一个很好的资源是SICP,它特定于Scheme(一种LISP方言),并在这里完整提供。 Once you've learned a few languages, it doesn't take too long to pick up the syntax and semantics of a new one (the core libraries on the other hand, can take quite a while to be familiarized with). 一旦你学会了几种语言,就不需要花太长时间来学习新语言的语法和语义(另一方面,核心库需要花费很长时间才能熟悉)。

If you want to learn about how today's computers work, I'd recommend learning C and reading books such as Tanenbaum's Modern Operating Systems. 如果你想了解今天的计算机是如何工作的,我建议学习C和阅读书籍,如Tanenbaum的现代操作系统。 C is useful in this context mostly for reading systems level code. 在这种情况下,C主要用于读取系统级代码。 Implementing a (very) simple operating system can be incredibly educational. 实现(非常)简单的操作系统可以非常有教育意义。 However, something as simple as implementing a basic shell (like bourne shell, except simpler) is probably a better place to start. 然而,像实现基本shell(如bourne shell,简单除外)这样简单的事情可能是一个更好的起点。 I'd also recommend learning about how networking works specifically, since it's such an integral part of modern computer systems. 我还建议了解网络如何具体工作,因为它是现代计算机系统中不可或缺的一部分。

C and C++ make some basic underlying programming concepts more evident, but they weren't designed by God. C和C ++使一些基本的底层编程概念更加明显,但它们并非由上帝设计。 I'd second the suggestion to study the actual low-level systems behind your low-level code: operating systems, compilers/runtimes (try "Essentials of Programming Languages"), and machine architecture. 我的第二个建议是研究低级代码背后的实际低级系统:操作系统,编译器/运行时(尝试“编程语言的基础”)和机器架构。

PS In general it may be better to study C++ on its own, rather than starting with C, but for your particular purpose -- getting more intimate with low-level, unsafe constructs such as pointers, after already learning Java -- I think it's better to start with C (and K&R) where these are front and center. PS一般来说,单独学习C ++可能会更好,而不是从C开始,但是为了你的特殊目的 - 在已经学习Java之后,与指针等低级,不安全的结构更加亲密 - 我认为它是最好从C(和K&R)开始,这些是前面和中心。

Learn C and C++ at the same time, I'm talking from experience here. 同时学习C和C ++,我正在谈论这里的经验。 Very often I come across code that mixes C and C++, so it's better to know both and their differences. 我经常遇到混合C和C ++的代码,因此最好知道它们和它们之间的差异。 Pick up K&R for C (understand pointers, header files and manual memory allocation and cleaning...which are not used by Java!) and any decent C++ beginner book (I picked Prata, but whatever you are more comfortable with). 拿起K&R for C(理解指针,头文件和手动内存分配和清理......这些都不是Java使用的!)和任何体面的C ++初学者书(我选择了Prata,但不管你感觉更舒服)。 Practice the same examples doing versions of C, C++ in a sequential fashion, object-oriented (OO) fashion, generic/template fashion etc. C++ has a larger standard library than C: templates, STL containers (no need for pointers, but you can do pointer fine tuning writing your own container), threads (since C++11). 练习相同的示例,以顺序方式执行C,C ++版本,面向对象(OO)方式,通用/模板方式等.C ++有一个比C:模板更大的标准库,STL容器(不需要指针,但是你可以做指针微调编写自己的容器),线程(自C ++ 11以来)。 You can always use C if you have no choice (or Boost libraries), any C++ compiler will allow it. 如果你没有选择(或Boost库),你总是可以使用C,任何C ++编译器都会允许它。

If you come from Java you should already know OO concepts for C++, and, perhaps, some generic programming as in C++ templates. 如果你来自Java,那么你应该已经了解了C ++的OO概念,也许还有C ++模板中的一些通用编程。 C++ is mistakenly regarded as an OO language, but it's more than that. C ++被错误地视为OO语言,但它不止于此。 BTW, objects are a dynamic concept (runtime), whereas templates are static (compilation time), so learn the language CONCEPTS, not just the syntax! 顺便说一句,对象是一个动态概念(运行时),而模板是静态的(编译时),所以学习语言CONCEPTS,而不仅仅是语法! Once you learn the concepts read Stroustrup's book (he created C++) to learn his philosophy for the best design rules for C++ code. 一旦你学习了这些概念,就会阅读Stroustrup的书(他创建了C ++)来学习他对C ++代码最佳设计规则的理念。

Learn the latest C++ standard (C++11) as it adds many new things to the language (auto, nullptr, threads, lambda functions, new containers, etc.). 学习最新的C ++标准(C ++ 11),因为它为语言添加了许多新东西(auto,nullptr,threads,lambda函数,新容器等)。 Last but not least, please use Doxygen in C/C++ the same way you used Javadoc....there is nothing worse than undocumented, unreadable code no matter what language you're using. 最后但并非最不重要的是,请使用C / C ++中的Doxygen,就像使用Javadoc一样......无论您使用何种语言,都没有比未记录的,无法读取的代码更糟糕的事情。

I would suggest learning assembly language first. 我建议先学习汇编语言。 This will give you a very solid foundation in what is happening at a low level. 这将为您提供一个非常坚实的基础,在低水平发生的事情。 This will also help to reinforce the idea that "everything is really just an address". 这也有助于强化“一切都只是一个地址”的想法。

Taking a class which focuses on assembly language is advisable since it will "force" you to learn it (personally, I don't think ASM is /that/ fun, but it was worthwhile [and a requirement for graduation] for me to take the class). 参加一个专注于汇编语言的课程是可取的,因为它会“强迫”你学习它(个人而言,我认为ASM不是/那个/有趣的,但我认为这是值得的[并且要求毕业]班级)。

After you know assembly, go on to C and C++. 知道汇编后,继续使用C和C ++。

Have a lot of fun! 玩得很开心!

It sounds like you're avoiding the first mistake most people make, which is assuming the new language is the same as the old one. 这听起来像是在避免大多数人犯的第一个错误,即假设新语言与旧语言相同。 C++ is different and should be learned as a neww(-ish) language. C ++是不同的,应该作为neww(-ish)语言学习。
A reference I would suggest would be C++ How to Program which is used at my University for the introductory C++ classes. 我建议的参考文献是C ++ How to Program ,它在我的大学用于介绍C ++类。

After that, then look at previous Java software that you have written and seeing how you would translate them to C++. 之后,再看看之前编写的Java软件,看看如何将它们翻译成C ++。 The syntax can easily be referenced from CPlusPlus.com . 可以从CPlusPlus.com轻松引用语法。 While doing this, it is important to keep in mind what all the different syntax represents, and how it changes what is going on in the software (ie The differences between the two languages). 在这样做时,重要的是要记住所有不同的语法代表什么,以及它如何改变软件中发生的事情(即两种语言之间的差异)。 This has the added benefit of allowing you to see how the underlying architecture is represented for both languages (and for programming languages in general). 这样做的另一个好处是可以让您了解如何为两种语言(以及一般的编程语言)表示底层架构。 I don't know of a good book that explains how programming languages work under the covers, or I'd recommend that. 我不知道一本好书如何解释编程语言如何在幕后工作,或者我建议这样做。

If you are, however, interested in learning how programming works, then Assembly language would be a good place to start. 但是,如果您对学习编程的工作方式感兴趣,那么汇编语言将是一个很好的起点。 Assembly language for Intel-Based Computers is what I used to learn assembly language and it was very useful. 基于Intel的计算机的汇编语言是我用来学习汇编语言的,它非常有用。

Assembly Language. 汇编语言。

Start with the Z-80. 从Z-80开始。 Then add 'x86. 然后添加'x86。 Then try 68000. Then the TI 320 series of DSP. 然后尝试68000.然后是TI 320系列DSP。 You might also wish to add the Z-8. 您可能还希望添加Z-8。 Just to see how different machines do it. 只是为了看看不同的机器是如何做到的。

Set up a performant C++ compiling environment such as Microsoft Visual C++ 2008 Express and go through all links in Bjarne Strousrup's The C++ Programming Language site, beginning with C ++ Style and Technique FAQ . 设置一个高性能的C ++编译环境,如Microsoft Visual C ++ 2008 Express,并浏览Bjarne Strousrup的C ++编程语言站点中的所有链接,从C ++ Style and Technique FAQ开始 If you are experimented in any other language you don't need more :-) 如果您使用任何其他语言进行实验,则不需要更多:-)

If you really want to know more about low level programming I would recommend learning C and Assembly. 如果您真的想了解更多有关低级编程的知识,我建议您学习C语言和汇编语言。 C++ is much more complex than C but really doesn't give you much more insight into low level concerns. C ++比C复杂得多,但实际上并没有让您更深入地了解低级别的问题。 It might be interesting if you want to learn about what type of concepts and constructs a programming language can be made up of though, since C++ has a lot of them. 如果你想了解什么类型的概念和构造编程语言可以组成,可能会很有趣,因为C ++有很多。

There is also a lower level of your VM you don't seem to know yet and which you might want to explore. 还有一个较低级别的VM,您似乎还不知道,也可能想要探索哪个。 To learn about the internals of Java I would recommend learning how to program the JVM in (Java) Assembly language. 要了解Java的内部结构,我建议学习如何用(Java)汇编语言编写JVM。 Jasmin ( http://jasmin.sourceforge.net/ ) is the reference assembler/syntax for this kind of programming. Jasmin( http://jasmin.sourceforge.net/ )是这种编程的参考汇编程序/语法。 Another great resource is the Java Language specification ( http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html ) which contains a lot of Java internals. 另一个很好的资源是Java语言规范( http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html ),其中包含许多Java内部。 When you have learned C you can also use some lower level APIs the JVM provides ( http://java.sun.com/javase/6/docs/technotes/guides/jvmti/ ) that allow you to retrieve low level information about a running JVM and write interesting things like debuggers. 学习C之后,您还可以使用JVM提供的一些较低级别的API( http://java.sun.com/javase/6/docs/technotes/guides/jvmti/ ),它们允许您检索有关a的低级信息。运行JVM并编写有趣的东西,如调试器。

If you learn this stuff and do some hacking on your own you will learn how the JVM works and what the compiler actually puts into your class files. 如果你学习这些东西并自己做一些黑客攻击,你将学习JVM的工作原理以及编译器实际放入类文件的内容。 It's also very likely that you discover some new things about the Java programming language itself you didn't know before even if you think you know everything about it. 即使你认为你对它的一切都知之甚少,你也很可能会发现一些你以前不知道的Java编程语言的新东西。

You can also program the .Net VM in assembly by the way. 您也可以在程序集中对.Net VM进行编程。

I think you should start with C, but not as a necessary preamble for learning C++. 我认为你应该从C开始,但不是学习C ++的必要序言。 Rather, for learning C. In other words, while you learn C put your efforts into learning the language, feeling the philosophy of the language and focusing on letting it sip into your skin. 相反,为了学习C.换句话说,当你学习C时,你会努力学习语言,感受语言的哲学,并专注于让它啜饮你的皮肤。 Be a good C programmer and you will be a good programmer, period. 成为一名优秀的C程序员,你将成为一名优秀的程序员。 Not just a good C++ programmer -- this has nothing to do with learning C -- but a good programmer. 不仅仅是一个优秀的C ++程序员 - 这与学习C无关 - 而是一个优秀的程序员。

There's another reason for learning C first. 首先学习C的另一个原因。 It's easier than C++, much easier than C++, and it bridges well to C++ (in contrast to Java, which doesn't in all aspects but the most superficial object-oriented ones). 它比C ++更容易,比C ++容易得多,而且它与C ++很好地搭配(与Java相比,Java不是在所有方面,而是最肤浅的面向对象的)。 I'm not talking about the syntax similarities: I'm talking about low-level programming. 我不是在谈论语法的相似之处:我在谈论低级编程。 I'm talking about the concepts of pointers, which exist as themselves and in the form of iterators in C++. 我在谈论指针的概念,这些概念本身存在,并且在C ++中以迭代器的形式存在。 You can pass around functions in C, and you can pass around function objects in C++. 您可以在C中传递函数,并且可以在C ++中传递函数对象。 C is fast to learn, and it will warm you up very effectively. C学习起来很快,它会非常有效地使你温暖。

Learning C will also eliminate the fear of free functions some pure OO programmers tend to have. 学习C还将消除一些纯粹的OO程序员倾向于拥有的自由函数的恐惧。 C++ is a hybrid language, and C truly is a subset of C++, not just by syntax but by philosophy as well. C ++是一种混合语言,C真正是C ++的一个子集,不仅仅是语法,还有哲学。

Start by getting yourself the K&R book and drinking it through. 首先,让自己获得K&R书并通过饮用。 You won't regret this. 你不会后悔的。

Whatever you start off with, my suggestion would be dump the full fledged IDE's. 无论你从什么开始,我的建议都是抛弃完整的IDE。 Use good text editors (vim/emacs) 使用优秀的文本编辑器(vim / emacs)

The learning curve is better when using text editors since everything needs to be written on your own. 使用文本编辑器时学习曲线更好,因为一切都需要自己编写。 No prompts and no pre-written code. 没有提示,也没有预先编写的代码。

You have all the best answers above, in anycase. 无论如何,你有上面所有最好的答案。 :) :)

  • Ivar 伊瓦尔

Learn Forth. 学习四。 It has better Objects. 它有更好的对象。 And it is a virtual machine. 它是一个虚拟机。 Unless you want a real machine see Green Arrays or Sandpiper/John Rible for that. 除非你想要一台真机,否则请看Green Arrays或Sandpiper / John Rible。

Free threaded interpreted versions are all over the 'net. 免费的线程解释版本遍布'网络。 For practice. 为了练习。 When you understand it write your own Direct Threaded version. 当你理解它编写自己的Direct Threaded版本。 Or see Forth Inc and buy one for your machine or use their free windows version. 或者参见Forth Inc并为您的机器购买一台或使用免费的Windows版本。

Java is a Forth/C hybrid so if you want to go Java you will have some of the stuff under your belt. Java是一个Forth / C混合体,所以如果你想要使用Java,你将拥有一些东西。

Education: 教育:

Starting Forth - Brodie Thinking Forth - Brodie 从第四位开始 - 布罗迪思考 - 布罗迪

The second is excellent for any language because it is the best book on factoring I know. 第二个对任何语言都很好,因为它是关于我所知道的因子的最佳书籍。 Free Versions of both on the net. 网上免费版本。

If you want to do a hardware/FPGA Forth Stack Machines: The New Wave by Koopman 如果你想做一个硬件/ FPGA Forth Stack Machines:Koopman的The New Wave

All the above books are free on the 'net. 所有上述书籍都在网上免费提供。

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

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