I need to learn C++ in order to learn building Nokia WRT and or maemo application. I need to know what gotchas and what aspect of C++ that I need/have to learn or focus more. One thing I got in my mind is that C++ doesn't have garbage collector. Therefor, I need to focus on variable type. But, is there any others that really important and I can't ignore it?


You might start with the list of C++ pitfalls: stackoverflow.com/questions/30373/…

Written by James McNellis

I think C++ is a lot more complex (for lack of a better word) than PHP, and you don't just learn the differences between PHP/Java and C++... You learn C++.

Written by crimson_penguin

yes, I know I HAVE TO learn C++. Maybe my wording is wrong. I just need to know which part of the language that I need to focus more so that I can focus my learning time on that particular aspect.

Written by silent

What to know when learning to drive a F1, if I came from driving Lada/Porsche?

Written by SyntaxT3rr0r

@Wizard: more like "I'm really into surfing and cycling. I've been invited on a biathlon. Any tips before I start?". Other than "take warm clothes" it's a bit difficult to pick a small number of issues :-)

Written by Steve Jessop

Accepted Answer

Main gotcha is to try to envisage C++ in terms of how it differs from PHP or Java.

Sorry, it just doesn't work like that. C++ differs from those languages in almost every important respect beyond the syntax for arithmetic. Sometimes the differences are subtle. You need to learn it fresh, and not think that something that's appropriate to do in PHP or Java will work well for you in C++.

That said, common difficulties include:

  • resource management: RAII; implementing copy constructors, destructors and operator=; avoiding having to implement copy ctors, dtors, operator=.
  • understanding what references, pointers, values and automatic variables are.
  • avoiding undefined behaviour (myarray[i] = i++; is a favourite). PHP and Java are both more "tightly" defined languages than C++: firstly the behaviour of a program is more likely to be defined and hence reliable. Because of this, separate implementations are more similar than C++ implementations. It's pretty easy to write a program in C++ that doesn't just do the wrong thing, it does wildly different things on different runs, including crashing, corrupting data, etc.
  • learning to safely and effectively use templates, multiple inheritance, operator overloading, and other features you're not familiar with.
  • correct idioms for throwing and catching exceptions (throw by value, catch by reference, don't throw out of a destructor).
  • writing portable code (understanding the difference between what the standard guarantees, and what isn't guaranteed but that your implementation happens to do. implementation-defined behavior such as the sizes of fundamental types).
  • C++'s standard libraries are limited compared with Java or PHP. You will be using non-standard libraries as well. For instance, Maemo uses GTK+ and/or Qt. Often the answer to "how can I do X in C++" is, "you can't do it using only standard C++, you need platform-specific APIs or a portable library compiled for your system". X can be graphics, sockets, regular expressions, multi-threading, XML handling, crypto. Especially with mobile platforms you need to keep an eye on OS versions, things can and will change under you from time to time.
Written by Steve Jessop
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki