views:

866

answers:

12

I have ~2 years of experience in Java programming, and it is basically the first programming language I have made serious and intensive use of. Currently, I need start switching to C++ for some assignments I have. Up until now I have been very comfortable with Java, and it's concepts made lots of sense to me.

Just off the top of my head: things like generics, calls by-reference, heap and stack allocation, useful APIs (Collections etc...), GC, strong typed varibles, etc... All of those made lots of sense in my head.

Now, needing to move to C++, what are the main points I should put aside, and start thinking about differently? How did any of you accomplish this? Common pitfalls? Good references? I'm not looking for any books or the like, just the most concise and relevant information I need to get up and running quickly in C++.

Edit: I should point out that I am basically aware of the major differences, e.g. the fact that C++ has no GC. I am looking for more in-depth tips and references to good material on this subject.

A: 

Memory management is the biggest thing you'll run into - it's not automagic any longer so you will have leaks, invalid memory accesses, buffer overruns, etc.

The concepts of pointers and indirection will also be fairly new as well.

ctacke
+3  A: 

The major difference between C++ and Java will be pointers (and references, they're not exactly the same thing) and memory management. Java hid these things from you, in C++ you'll have to do it yourself. This allows for greater flexibility, but with great power comes great responsibility. It's all too easy to run into memory leaks. I'm afraid you'll have to change your thinking a bit.

The rest is more or less a thing of a slightly different syntax and common libraries. You should be able to pick that up easily as you go along.

Vilx-
Only difference between java references and C++ pointers is that you cannot apply arithmetic operations on java references. C++ references are completely different from java references.
David Rodríguez - dribeas
A: 

Since C++ doesn't have GC, you should remember to deallocate any memory you allocate. For new'd memory, use delete operator.

new:
int* ptr = new int; // pointer to an int
delete ptr; // get rid of it

As per the comments, it'd be worthwhile to look into Smart Pointers.

Will Mc
don't tell people to use malloc in C++char* ptr = new char[10];delete[] ptr;
joshperry
Don't use delete or delete[] in C++. Use a smart pointer or a wrapper class.
Eclipse
? I agree it's easier to use smart pointers / wrapper classes if you are familiar with them, but it's important to understand the base language mechanism for memory allocation.
Jason S
That's what I was going for originally. Also why at first I had malloc in there.
Will Mc
+4  A: 

Most of all, be aware of the superficial similarities between the languages. Thay do have somewhat similar syntax, but very different semantics. Don't try to do things "the Java way" or you'll get frustrated quickly. Better try learning C++ from a good book (such as "Accelerated C++", IMHO).

And frankly, I don't think there is a way to "get up and running quickly in C++." unless you are coming from C. You'd better learn the language well before starting anything serious with it.

Nemanja Trifunovic
Agreed. "Doing things "the Java way" is probably the biggest pitfall there is.
jalf
A: 

As other answers have pointed out, memory management is one of the big differences - however you can get garbage collectors for C++. For example, see here (no idea of the quality of this one, so it would be worth researching to find the best one).

The other issue is that C++ is close to C (and can be C if you try), which removes a lot of checks you are used to (e.g. bound checked arrays).

However all of these issues can be overcome with use of good class libraries. So your biggest obstacle will probably be the different syntax and semantics, especially regarding pointers and references, operator overloading, etc.

Good luck.

frankodwyer
I've coded in both back and forth, though mostly Java, and the biggest hurdle I have is that Java is really a "platform" and C++ is just a "language." Java gives you a lot of built in collections, classes, etc, and C++ does not.
tmeisenh
+15  A: 

I think the best course of action is assume you know nothing and learn C++ from scratch.

The memory management thingy is just the tip of the iceberg (and is not really a big deal any more with smart pointers and RAII techniques). Don't focus on this. Memory issues exist in all programming languages, they just appear at a different point.

I would say:

  1. Learn the C++ syntax, references, pointers, classes. Fortunately control structures are almost identical to Java ones so you will go very fast for this. The C++ Language is a good reference book.
  2. Read Effective C++
  3. Learn templates
  4. Learn the STL - The STL is generic programming oriented, this is different from a typical Java Lib
  5. Take a look at the boost libraries (http://www.boost.org/)
  6. You will now realize that you didn't understand the true power of templates and will want to learn them again :)
  7. Read Sutter's books - Exceptional C++ style for example
  8. Read Modern C++ Design book
Edouard A.
+1 on the selection of books.Java generics and templates are far away cousins. Some of the things will look familiar, but there is much more in templates than java generics can offer (specialization, SFINAE...)
David Rodríguez - dribeas
C++ For Java Programmers by Mark Allen Weiss (be certain you have this author's book) is a great starter to see what key differences you should be aware of, and as a starter to the other fine book mentioned here.
Spencer K
You will also find the C++ FAQ Lite useful as an online reference, also: http://www.parashift.com/c++-faq-lite/
Greg D
Actually all the "red collection" books are nice to read once you have reached a certain degree of understanding.
Edouard A.
@Edouard A.: What are "red collection" books?
Lazer
@eSkay : It's the "C++ in Depth" series by Addison-Wesley Educational Publishers
Edouard A.
+6  A: 

You'll really want to look at Scott Meyer's books Effective C++ and More Effective C++. These should really be required reading for anyone wanting to program in C++. Take a look at the C++ FAQ. These resources are all about how not to shoot yourself in the foot. C++ isn't going to protect you from yourself, so you're best off learning a few rules at the beginning until you know when you're allowed to break them.

RAII is probably the single most useful technique in C++ (and also one of the most idiomatic and really doesn't show up in other languages much). See this question for some explanations on how RAII replaces finally in other languages.

Herb Sutter has also written a ton of useful stuff looking into the gotcha's and problem cases in C++. Particularly helpful are the GotW series.

Eclipse
finally is what the language must offer when it cannot offer deterministic destruction. +1 on RAII
David Rodríguez - dribeas
+2  A: 

C++ and easy should not probably be used in the same sentence; its like get-rich-quick, a scheme.

I would give you two pieces of advice, coming from C# to C++:

  1. USE THE STANDARD LIBRARY!!!

  2. Read Bjarne Stroustrup, The C++ Programing language; he's a great writer(who also created C++) and emphasizes problems solving and correct methods over language minutia.

Also, for a lot of extended libraries look at TR1 and boost. TR1 is a new amendment to the standard library, and a lot of TR1 came from boost.

joshperry
If you read Stroustrup, be sure to read the Special Edition (the big fat one which covers STL and generic programming to a decent extent), http://www.amazon.com/C-Programming-Language-Special-3rd/dp/0201700735
Jason S
+3  A: 

Familiarize yourself with the standard library, and learn to appreciate it. It's radically different from Java's class library, but most of it is actually an amazingly well-designed library (unlike most of Java's class library ;)).

Learn RAII, avoid manual memory management, understand templates (and not least, the differences between C++ templates and C#/Java generics), and take a look at what's possible with template metaprogramming. (You probably won't need to learn that yet, but getting a brief overview of it may be helpful, and help illuminate other parts of the language).

Don't try to do things "the Java way". Work with the language rather than trying to make it something it isn't.

Read this.

Get a copy of the standard (or a draft, because they're free, and nearly identical), so you can look things up as necessary.

The main thing I'd worry about is undefined behavior. There are many instances of C++ code which would have been perfectly valid in Java or other languages, but which in C++ depends on undefined behavior, meaning that anything might happen when it is executed. Unfortunately, these problems are obviously hard to spot unless you already know about them (the entire problem is that it looks like valid code, it compiles with no warnings, and it seems to work when you test it)

jalf
lol, one minute ago i posted a link to your great answer for that C# guy having search for it 5 mins long (and then found it immediately with google hehe). then i saw your own answer :) of course now there is no need for my post anymore
Johannes Schaub - litb
+3  A: 

With most languages, a good programmer can just jump in and start going by fixing bugs.

The transition from Java to C++ is probably one of the cases where I'd say that's just not going to work. Debugging and dealing with pointers and references in C++ offer a completely new set of challenges and need some serious retraining--most of the comments here cover that.

I just wanted to say that you should be prepared for a fairly high level of frustration, but at the same time you should come away knowing much more about your computer in general.

Bill K
+1  A: 

Bill K is right on point. Books are great for references and reading some code helps too. You might want to get some chops by working on an open source project, Apple's WebKit would be good, and try fixing a few bugs.

link text

tmeisenh
A: 

First I would ask what you need C++ for. Do you want to get a real C++ pro or do you just need to use it in a specific domain?

Of course, there is some basic c++ knowledge that you will need in every domain. But I guess you will get those basics anyway.

On top of the basics, there is way too much to learn it all at once. Depending on the Operating System you write for, the kind of application (command line application, GUI, plugin, library, etc.) you will have the possibility or the need to use certain libraries, which may force their programming style on you. For example, writing cross-platform plugins for the renderer "mental ray" and writing GUI applications with Borland C++ Builder seemed like two distinct worlds to me, though both are c++. Perpare to enter a new world with every new project you begin.

Knowing Java has its pros and cons. You might have a better understandig of OOP than most C++ veterans, but getting memory management right might be difficult. Not to mention templates, which are very powerful but IMHO too complicated for the human brain to take full advantage of.

Once you know C++ you might be interested in C#: It's syntax and concents are like Java, but you can embed C++ code just inline.

Brian Schimmel