Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've done Java for ten years, and stopped doing any C++ sometime in the mid-90's.

What's the best way to quickly relearn C++?

Near as I can figure, the language has changed significantly in 15 years; or, writing good 1995 C++ isn't the same as writing modern good C++. RAII is one thing that keeps coming up, and I still don't quite know what it is.

share
2  
I would start with the definitive C++ reading list: stackoverflow.com/questions/388242/… –  Doug T. Sep 25 '10 at 21:34
    
There is a really good example on RAII in an answer to a question I had about it: programmers.stackexchange.com/questions/29692/… –  Felix Dombek Jan 20 '11 at 1:04
comments disabled on deleted / locked posts

locked by maple_shaft Feb 11 at 16:27

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

13 Answers

IMO, the real question isn't "What should I do?", but "Which book should I read?" Thinking in C++ has already been mentioned, and it's a reasonable one. Under the circumstances, I think I'd probably go for Accelerated C++ instead though. Where Thinking in C++ mostly assumes you know nothing about programming and starts from the beginning, Accelerated C++ assumes you've programmed in some other language, and have a reasonable idea about programming in general. Rather than start from the very beginning, it just tries to teach you how to use C++ to do the kinds of tasks with which you're already familiar.

share
5  
Accelerated C++ is worth reading occasionally even if you have been using C++ for the last 10years –  Martin Beckett Oct 3 '10 at 21:41
3  
+1: I read a few books while learning C++, but after Accelerated C++, I finally "got it." I then decided that I didn't want it, but that's another story. –  Larry Coleman Nov 11 '10 at 13:49
1  
+1: Accelerated C++ is also much more recent and (because of that) in tone with modern C++ style and idioms. Thinking in C++ is 11 (Vol 1, 8 Vol 2) Years old, and it shows. –  Fabio Fracassi Aug 3 '11 at 9:38
add comment

I think that these books are by far the best way to get up to speed on all the itty bitty details you need to keep track of when coding C++:

share
    
Even more effective C++ –  user1449 Aug 3 '11 at 12:41
add comment

Boost! Learn Boost, it's probably the most important thing you can do to bring yourself up to modern C++. Other than as mentioned by other people read the books by Andrei Alexandrescu, Scott Meyers, and Herb Sutter. Also, read Herb Sutter's blog.

share
add comment

"The best way" and "Crashcourse" don't combine well so I'm going to provide what I think is the best way:

Thinking in C++ 2nd Edition by Bruce Eckel
is a very good book if you already know a programming language and you need to learn C++ by heart.

If you still have sufficient knowledge about C++ a tutorial site (eg. Wikibooks - C++) might be better.

share
2  
+1 : I've relearn C++ starting whith this book and it's sequel. I lost confidence in all my programming skills because of ways of doing things that were all evil in a company I worked for years (I was young). After having failed an interview for a more interesting C++ job, I thought I should just get back to basics and go through this book. After reading the book I felt more confident in my skills because it explains fundamental things about C++ that your Java background might never make you understand at first sight. Anyway I recommand it for getting back to C++. –  Klaim Sep 24 '10 at 13:16
add comment

In the last years C++ development has focused a lot on metaprogramming. This is an aspect that is totally absent in Java but is growing in popularity among experts C++ programmers. I understand this is more about design than about the language itself but nowadays it can really sound strange if you claim to be a C++ developer but you've never heard anything about metaprogramming.

That said, I'd re-start with the Stroustrup book, followed by this book.

Also learn the boost library and stay tuned on the the C++0x standard, which grabs a few things from the boost library.

share
add comment

It will require a (small) paradigm shift.

Going from Java to C++ means:

  1. Raw pointers.
    • It works the same way in 2010 as in 1995.
  2. No garbage collection.
    • Need to understand the various ways of resource cleanup without GC, and their limitations.
      • RAII
      • Reference counting
  3. The importance of destructor.
  4. How smart pointers encapsulates these resource cleanup methods
    • Use the appropriate type of smart pointers for resource.
  5. How resource management, sharing and smart pointers affect API design
  6. How RAII, smart pointers and exception work together
  7. Pick up the standard libraries, including STL. Also need to pick up platform-specific APIs, maybe a couple other libraries because the standard library is not as powerful as Java's.
  8. Learn just enough C++ templates to be able to use the libraries.
    • Similar to Java generics

RAII basically means the lifetime of a resource is either:

  • Same as a C++ object, by putting the resource into a class member, initializing it in the constructor (or an initialize function) and releasing it in the object's destructor,
  • Same as a scope, which may be a function or some code surrounded by braces {}. This requires the resource be encapsulated by a C++ object, so that it is released by the destructor at the end of scope, regardless of normal or exception.

RAII requires a more hierarchical ownership structure. It does not allow sharing resources between two objects where one may be released independently of another, unless the two objects are in turn owned by the same higher-level object.

Reference-counting allows sharing resources between objects having independent lifetimes. However, care should be taken to avoid cyclic references when using reference-counting.

There is something called Boehm GC (mark-sweep) which tries to bring back the joy of automatic garbage collection to C++, but it's not a silver bullet.

The learning process is much easier and productive if the first few months of going to C++ is spent working on an existing "Clean" C++ code base, which uses the main C++ features without touching the difficult concepts. (I took 6 months.)

If the project involves multi-threading, porting will be much more difficult.

Edited You probably need to learn goto because there is no equivalent of labeled break in C++.

share
add comment

Bjarne Stroustrup wrote "Programming - Principles and Practice Using C++" which is a great book, using C++ to teach programming.

It might be a good way to start relearn how to use C++ in a good way.

I would also advise to read Bjarne Stroustrup other book the C++ Programming Language, which is not only a reference book but one that I really enjoyed to read.

share
add comment

Even if the language had not changed, I would still recommend you to pick up a book or read a good online tutorial (but C++ is a big language, so that's one long tutorial to have to read on-screen). A lot will have been forgotten in those 15-20 years.

The differences between Java and C++, as you may know, are vast despite their similarities in syntax. This makes it easy to introduce practices which work well in Java but may be very inefficient or even disastrous in C++. Especially when you've worked a long time with one but not the other.

So do yourself a favour and make the effort to relearn C++ "from scratch". Don't just jump straight into coding; it will save you a lot of headache. And memory leakage. Also, you will hopefully not have to face the following scenario:

-WTF is this piece of code?! Hey! You there! Did you write this? What the hell were you thinking?!

-But that's how you do it in Java...

-THIS ISN'T JAVA, STUPID!

share
1  
with the help of a good book like C++ Primer, I advocate jumping straight into coding as soon as possible, (alternatively, get a small C++ project source code and delve into it) because many concepts are difficult to learn from theory. As long as the code doesn't go into production. –  rwong Oct 24 '10 at 17:16
    
@rwong: Provided this coding is done along side the book, then I'd say this is indeed a good idea. +1 –  gablin Oct 24 '10 at 21:14
add comment

I would recommend following the C++, C++0x and related tags over on SO. Make sure you understand each question and each answer, read the linked documents that are almost always in the answers, search to learn more about RAII and other acronyms that pop up consistently, and basically drag yourself forward until you feel you could answer many of the questions.

This has the advantage of feedback. If you "just start typing" you will not have any way of knowing if you're using appropriate idioms. It might compile, but be very old-school or just not C++-ish. If you read a book, it might be outdated. But if your "I knew that" meter is rising over time spent in those tags, you know you're starting to get it.

share
add comment

Here's how I've learned (the basics!) of C++ in quite a short amount of time:

  1. C++ FAQ
  2. Effective C++
  3. Stackoverflow
share
add comment

this might be a good idea ... translate or rewrite one of your JAVA programs in C++? Since you already know the logic, you are not actually solving any problems (or learn to solve prob) per se .. in this case you're just re-learning the language ...

share
3  
I don't agree here. C++ can be a tricky language. There's ways to do things safely, but they aren't all immediately obvious to somebody who last touched C++ in the 90s. Moreover, such a person wouldn't necessarily be aware of what can be done with algorithms and containers and smart pointers. –  David Thornley Sep 24 '10 at 15:09
    
What the heck is a smart pointer? –  Dean J Sep 24 '10 at 15:19
    
Well - my suggestion is to rewrite a Java app with the current C++ language. Also, it doesn't have to be a straight code translate, it could be coding a concept (say i've created a small calendar system in Java), let's port it over to C++ –  aggietech Sep 24 '10 at 15:29
    
@Dean J: a smart pointer is a pointer that can free the memory that has been allocated to it when it is no longer used. –  Matt Ellen Oct 14 '10 at 10:20
4  
@Dean J: knowing what a smart pointer is, what problems it solves, roughly how it's implemented, is key to knowing modern C++. For extra points, know what's wrong with the old STL auto_ptr and how things are better with shared_ptr, weak_ptr, unique_ptr etc. Also why make_shared couldn't be implemented without rvalue references... but that's pretty advanced. –  Kate Gregory Oct 24 '10 at 18:18
add comment

just start coding! find a project, say something open source, and start typing! Google is your friend, there are tons and tons of tutorials, e-books, and sample code out there!

share
6  
There's too many ways to do too many things wrong in C++, I'd advise looking at a modern reference. –  Matt Olenik Sep 24 '10 at 14:09
    
You really do need a reference book to help you understand all the things you cand do wring –  Casebash Sep 24 '10 at 23:19
add comment

I just did the same myself recently and as @Arlaharen recommends I read Scott Meyers books (although I'd probably skip More effective c++).

I'd also recommend these to get down and dirty

I found templates and the STL a lot clearer after these.

share
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.