C ++ from Java view: I must have missed a few things

First of all, let me first clarify that the thoughts below are purely my personal opinions and because of my limited knowledge. I have no intention of saying that C ++ is not cool.

I programmed C ++ as a year, and I think it really has interesting features. However, I feel a little empty and disappointed, because in fact I have not studied any “mind-altering” things with C ++, from the point of view of a person who previously studied Java (as the 1st language).

According to many posts I read, people prefer C ++ as it is faster. For a programmer like me who has not previously been programmed with temporary critical applications, I still have a chance to appreciate this.

So far, what I learned seems to be about the syntax. So we write a class in Java, but here's how to write it in C ++. This is how to do inheritance in Java and what to do in C ++ and so on. (I know multiple inheritance is cool, but for me it's not a mind-altering thing. I think it's great to be able to answer why Java does not / cannot support multiple inheritance, which should be more general than single inheritance).

Somehow to me all these are just syntaxes, and my mind doesn't seem to have developed after C ++ coding. I think my problem is to write C ++ programs using Java-mind. What I really want, as many people suggest, is to change my thoughts about thinking after learning a new language. I still do not understand what is happening with my C ++.

I can also write a couple of small Python programs. However, I am afraid to learn more about this, as I am, again, it will just be learning a new syntax, a new way to do things that are just different, not knowing the reason.

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

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

PS: By the way, in C ++ there is one specific question that I want to confirm. In C ++, writing as follows is inefficient if I am right:

private A computeAndReturnA(){...} 

Instead, write it as:

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

as in the first case, the returned values ​​are copied (when we assign b = compute ...) and introduce some inefficiencies? (In Java, I assume that the first method is clear in meaning and good in efficiency, as it passes things by reference)

+7
java c ++ programming-languages
source share
8 answers

You are completely mistaken, in short. The fact is that C ++ offers a HUGE amount of freedom compared to Java.

For example, you can allocate classes on the stack. Java does not offer this. You can calculate specific values ​​at compile time. Templates offer much more power than generics. You have the ability to make something a reference or value. In Java, all of these options are taken away from you. For example, in C ++ you can extend several classes. You must not expand the object. A deterministic resource is cleared if you want. I could go on, go on and on.

If everything you do is exploring the syntactic options, then it is entirely possible to use C ++ in a somewhat acceptable mode. However, C ++ offers dozens of things you'll never see in Java.

The simple truth is that Java is more like a subset of C ++ with a larger standard library, plus possibly reflecting and generating code at runtime.

I prefer C ++ because Java, frankly, is full of arbitrary restrictions. Do you know why there is no friend instruction in Java? Because James Gosling thought it was against his principles. Fine. This is amazing. Now I need to divide my implementation into two classes and pretend that these are two implementations with separate encapsulation, because he thought it was wrong ten years ago. This is my personal example of why Java is so high - you program as James Gosling says, not how you want, or, in many cases, how you really should.

In addition, I looked at your PS. This is why C ++ has the right compiler. The reality is that almost all compilers will turn the first form into the second for you, and some other bending optimizations that you don't want to know about are done behind the scenes.

+6
source share

Learning C is your best option here - it will lead you to a bare bone, which forces you to not use your way of thinking and, as such, you can easily and easily switch to C ++.

+2
source share

The simple truth is that it takes quite a long time to learn how to think in a language, for example. best practics. This is a long process when you start by learning the syntax and probably first apply the language paradigms first to find your way.

It is true that if you choose a language that is very different from Java syntax, as well as a paradigm, you may notice these differences earlier, but not underestimate C ++ as another paradigm.

C ++ is a language that is very difficult to learn, however it is a language that can create the impression that it is easy to learn, especially because the syntax is similar to several other languages, but there is something more to it. One of the main topics is, for example, programming templates, using a template is quite simple, but if you look at the boost template library, you will be quite complicated, and creating such a library is not for the faint of heart.

Learning a language is simply not learning syntax, it is learning how to think in that language.

However, C ++ is one of the languages ​​that allows you as much freedom and control as possible, and sometimes too much freedom.

+2
source share

Definitely take a look at www.boost.org, it will be a change of mind.

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

In current C ++:

Firstly, if the compiler supports RVO, and especially when it also supports NRVO, there will be no chance of a high copy. Then it depends on how the function is written:

 A computeAndReturnA() { return A( x, y ); // no copy almost for sure } A computeAndReturnA() { A a; af(); ag(); return a; // no copy if compiler implements NRVO } A computeAndReturnA() { A a; while ( condition1 ) { af(); if ( condition2 ) return A(); // copy will take place } ag(); 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 the upcoming C ++ (March 2011?):

The same arguments are used as for the current C ++. However, if this reasoning assumes that a copy will be made, and if the class (A here) has a movement mechanism and moves the assignment operator, then "move" and not "copy". In ideal cases, the movement will be eliminated, and you will get a direct construction, but if not, you will get the movement in the worst case, of course, provided that the returned class is aware of the transfer.

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

+2
source share

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

In C ++, you certainly can do a lot of things that you could not dream of in Java. However, for those who have a Java background, I would say that C ++ is a very poor learning tool, since it is too easy to code a few delete instructions here and there in Java.

If you need some kind of mind that changes experience, you should learn a language that actively emphasizes a different paradigm, rather than a language that allows you to choose any paradigm you want.

I would suggest one of Haskell (functional programming), Erlang (unusual concurrency approach), Factor (stack-based programming), Prolog (logical programming), Common Lisp (CLOS and macros), Javascript (OOP based on prototype, etc.) e. without classes).

This, in my opinion, would be a mental learning process. I would say that despite the fact that they support many things that Java does not have, Python and Ruby are still pretty close to the Java model; indeed, with a Java background, it would be easy to see them as just another skin in the same model, with slightly different syntax and type declarations.

I want to say that if you want to blow your mind, you need to learn a new paradigm. And although, of course, you can learn new paradigms in a fairly powerful language such as C ++, it will be much easier to learn a new paradigm by learning the language that will force you to use this paradigm.

For a quick overview of some mind-altering ideas, you can watch these video lectures .

+2
source share

If you are trying to learn new languages ​​to improve your programming skills, maybe try and learn something that is very different from the C style syntax. Perhaps one of the functional languages ​​such as Haskell, Scala or Erlang. This will show you how different programming languages ​​can be, and you will also begin to understand the subtle differences between Java / C ++ / C.

Otherwise, if you want to solve a specific problem, you can choose a language for this. If you do not have something important for performance, it is probably easier to stick with garbage-collected languages. If you do not plan to develop iPhone, in this case you should switch to Objective-C

+1
source share

To your question: The upcoming C ++ 0x will have movement semantics, which should accelerate the "return by value" to the comparable "return by reference" performance. They do not need to change the client code, but it may be necessary to implement an additional constructor in the moved classes.

About your Rant: read “Modern C ++ Design: General Programming and Design Patterns” by Andrei Alexandrescu, and I’m surprised how much C ++ and Java (and the corresponding thinking) can differ.

+1
source share

The reason you probably don’t feel that C ++ offers you any kind of candy for your mind is because you come from a Java background, which in every sense and purpose offers a very simple and simple C ++ translation . Not the best C ++, but possible work in C ++ ...

... The reason for the different idioms in your example PS has nothing to do with links (which Java does not have: Java has pointers to objects, but not links, but C ++ has links, as well as pointers) and all that is connected with the fact that in memory allocation, Java is relatively cheap, while in C ++ it is not (because in Java, the cost of allocating memory is amortized over everything that the VM should allocate anyway - where in C ++ it is not in Java, the VM guarantees that memory will be allocated as necessary in any case).

So, in effective C ++, you see that people avoid many redundant new calls in a narrow cycle, while in Java people can comfortably create the temporary objects that they need.

+1
source share

All Articles