Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 632
Paul Hoang Avatar asked Sep 01 '10 10:09

Paul Hoang


2 Answers

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.

like image 101
Puppy Avatar answered Sep 21 '22 10:09

Puppy


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++.

like image 39
alternative Avatar answered Sep 20 '22 10:09

alternative