Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am going to have a lecture about C++ for experienced Java programmers. Because C++ is a big language and I have just one lecture, I want to point out the most important "gotchas" and C++ features when migrating from Java to C++.

I myself went the opposite way (C++ -> Java) and therefore I would like to hear different opinions on this topic. What would you mention if you were me?

The to-mention list I have so far:

  • Use cases for C++ (when to use C++ instead of Java)
  • Compiler stages (preprocessor, compiler, linker)
  • Differences in data types (atomic data types, arrays, structs, classes, unions, enums), automatic conversion
  • Pointers and memory management, difference between pointers and references, stack vs. heap allocations
  • Interfaces and inheritance in C++ (multiple inheritance, virtual functions and the need of virtual keyword, pure virtual functions)
  • Rewriting generics to templates
  • STL (thanks Péter)
  • Operator overloading
  • Basics of macros

EDIT: added suggestions from the answers

share|improve this question

closed as off-topic by durron597, Snowman, ratchet freak, Ixrec, gnat May 11 at 20:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking career or education advice are off topic on Programmers. They are only meaningful to the asker and do not generate lasting value for the broader programming community. Furthermore, in most cases, any answer is going to be a subjective opinion that may not take into account all the nuances of a (your) particular circumstance." – Snowman, ratchet freak, Ixrec, gnat
If this question can be reworded to fit the rules in the help center, please edit the question.

Wanted: answer about RAII from someone who actually knows it. (Not me.) – Sean McMillan Sep 28 '11 at 13:16
Hi Karel, any chance you could share your presentation with us? Maybe slides or link to Youtube post? – Jose Chavez Jan 14 '13 at 17:07
Hey, it's not in English, I doubt it would be of much use to you. It's also just supporting slides for my talk, not a complete guide. – Karel Petranek Jan 15 '13 at 18:54

3 Answers 3

Your list sounds fairly complete to me.

On top of these, I would definitely mention the C heritage and the history of how C++ evolved from C, as it explains a lot of its idiosyncrasies.

Related to this, I would also quote Scott Meyers in that C++ is actually four distinct languages:

  • C
  • the object oriented C++ constructs
  • templates (metaprogramming)
  • STL

Last but not least, C++ was designed on the philosophy that users should pay only for what they use - in other words, performance was/is paramount, at the cost of safety. This is very different from Java which was meant to be (relatively) safe from the start, raw performance being lower priority.

share|improve this answer
I totally forgot about STL, thanks. Mentioning the C -> C++ evolution is also a valid point. To the last point: I think they pretty much understand this, I'm not sure if it is necessary to state it explicitly. – Karel Petranek May 9 '11 at 8:46
@dark_charlie, what I think I should have made more explicit is that - contrary to Java, where the JVM puts a safety net below you - in C++ there is a lot of things you just have to know in order to write correct code. And if you don't, your program will crash at unexpected places or do obscure things and you may have a lot of trouble figuring out why. I think this may be hard to comprehend without real life experience. You could show them some concrete examples, like what happens when you (even implicitly) copy an object containing pointer members without a proper copy constructor, etc. – Péter Török May 9 '11 at 8:57
éter: Thanks, now I understand you. – Karel Petranek May 9 '11 at 18:13

IMO, by far the biggest difference between C++ and Java is that Java uses dynamic dispatch (virtual methods) for all instance operations. A Java programmer that uses inheritance in C++ will wonder why his/her program is always calling the base class method.

Then there's the difference between references and pointers. It's probably safest for a Java programmer to always use pointers, as long as s/he remembers to delete them.

Except ... operator overloading pretty much assumes references. So you might want to talk about that, and why you have to do operator overloading with C++. Which takes you into the realm of copy constructors versus operator=.

At which point, any sane Java programmer should say "why do I want to know this?"

share|improve this answer
Not true! private methods are statically dispatched, final methods are statically dispatched, static methods are statically dispatched, and if the runtime detects that a class Foo has no subclasses or that an interface Foo has only one implementation, then all method calls on Foo are statically dispatched. – fredoverflow May 9 '11 at 11:17
Also, using pointers in C++ just because they resemble Java references is a terrible guideline. There should rarely be a need to call delete manually in modern C++ code. – fredoverflow May 9 '11 at 11:18

IMHO, you should also talk about allocating objects on the stack in C++. The idea of creating an object without using new would probably seem strange to a java programmer.

share|improve this answer

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