What Gotchas When Learning C++, If I came from PHP/Java?

674 views Asked by At

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?

6

There are 6 answers

0
Steve Jessop On BEST 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.
0
user207421 On

I'm curious as to why 'PHP/Java' is regarded as a single thing here. It isn't.

  • The Java->C++ transition is rather large but it is doable, you just have to learn a lot of extra syntax, a few extra concepts like destructors, copy constructors, object slicing, and operator overloading, and come to terms with the C++ library.
  • The PHP->C++ transition on the other hand is another order of magnitude larger, as it concerns (a) a very poorly-defined source language, (b) a language which is template-based rather than class-based, and (c) a language which executes in a special environment.
1
jwismar On

I would say it's not variable type, so much as making sure you clean up your memory. Java will clean up your memory, but C++ won't do it for you. Otherwise, managing your resources in the presence of exceptions is important.

On the plus side, you're going to get what they call "deterministic finalization." Huge benefit. Look up, as an acronym, "RAII". I think it's arguably one of the most important idioms in C++.

It stands for "Resource Acquisition Is Initialization", but what it really means is "When this destructor fires, I will clean up after you, even in the presence of exceptions." In practice, it means, any object that you create, or open, that you need to close or delete, you can hold using a smart pointer. The smart pointer will clean up after you. This is very, very powerful once you understand it and start using it. It makes your error checking, exception handling, and resource cleanup code very straightforward.

0
mrjoltcola On

Learn to use STL containers right away. While the iterator syntax isn't friendly for people coming from other languages, it gives you the builtin data structures that you generally use in Java's collections or PHP as builtin, (maps/hashes, lists, stacks, vectors) without writing funky pointer code with dynamic allocation that so often sidetracks beginners into reinventing the wheel and messing with memory bugs.

Also, if you write platform specific code (a Qt or Microsoft MFC app, for example) you'll often see examples using framework specific containers where the STL would be a wiser choice. STL (and Boost) can fill in gaps. Use of a GUI framework does not mean you have to use everything that framework offers. Stay away from non-standard containers unless you know without a shadow of doubt you'll never port the program or reuse parts of the code.

0
Peter Alexander On

The most important thing (in my opinion) is that everything is a value type, so if you pass an array into a function like this:

void do_stuff(std::vector<int> my_array)
{
  ...
}

then the my_array that is passed in is a copy of the argument specified. Copying an entire array like that is expensive, so in general, you want to pass by const-reference:

void do_stuff(const std::vector<int>& my_array) 
{
  ...
}

(Note: omit the const if you want to modify the original my_array).

0
Dan On

One gotcha I've seen some Java Programmers make when transition to C++ is "try-catch" memory leaks. For example:

try {
  myType pVar = new myType();
  DoSomething(pVar);
  delete pVar;
}
catch (exception) {
  cout << "Doh!  Something failed!" << endl;
}

In the case above, if the "DoSomething() method throws an exception, the pVar would never get deleted, so there would be a memory leak.

(Solutions to this include: declaring all variables before try/catch blocks, using auto_ptr, or just avoiding try-catch to begin with!)