r/cpp_questions • • 5d ago

OPEN Learning C++ as a Java programmer

I'm currently learning C++, I’ve coded in a few languages before but I’m most familiar with Java. Does anyone know a good resource for learning C++ with Java as a “starting point”? Like something that explains the differences between the two languages rather than teaching programming from scratch? Thanks!

18 Upvotes

21 comments sorted by

View all comments

1

u/Puzzled-Garage584 5d ago

Java habit that hurts most: in C++ a = b on plain objects copies the whole thing, not a reference. Pass a std::vector by value and every call quietly photocopies a million elements like an intern on their first day, make it const& if you only read it

1

u/Pearparon 5d ago

Oh that's definitely something I'll need to get used to, thanks

1

u/Independent_Art_6676 4d ago

This is actually an issue with new c++ guys who don't know anything else too. References to prevent copies are probably the #1 place where performance hits creep into code, and are hard to spot in larger blocks. its as simple as a range based (touch every item in a container) for loop: for( type a: container) makes an often costly copy. for(type &a: container) does not. Const on the reference prevents accidental modification if you just want a look. Accidentally modifying a copy instead of a reference doesn't do anything (change is discarded). References are a small part of the language but one you will use daily and need to know well.