What is Gotchas learning C ++ if I came from PHP / Java?

I need to learn C ++ to learn how to create Nokia WRT and maemo applications. I need to know what gotchas is and what aspect of C ++ that I need / need to learn or focus more. One thing that I thought of is that C ++ does not have a garbage collector. So I need to focus on the variable type. But are there others that are really important, and I cannot ignore it?

+4
source share
6 answers

The main way is to try to introduce C ++ in terms of how it differs from PHP or Java.

Sorry, this just doesn't work. C ++ differs from these languages ​​in almost all important aspects, in addition to the syntax for arithmetic. Sometimes the differences are subtle. You need to learn it fresh and not think that something that is suitable for PHP or Java will work well for you in C ++.

However, common difficulties include:

  • resource management: RAII; implementation of copy constructors, destructors, and operator= ; avoiding the need to make copies of ctors, dtors, operator =.
  • understand what references, pointers, values, and automatic variables are.
  • avoid undefined behavior ( myarray[i] = i++; is a favorite). PHP and Java are more β€œhard-coded” languages ​​than C ++: first, the behavior of the program is likely to be determined and therefore reliable. Because of this, individual implementations are more similar than C ++ implementations. It is very easy to write a C ++ program that not only does the wrong thing, but also does different things on different runs, including crashing, data corruption, etc.
  • Learning how to use templates in a safe and efficient way, multiple inheritance, operator overloading and other functions that you are not familiar with.
  • the correct idioms for throwing and trapping exceptions (throw by value, catch by reference, do not throw from the destructor).
  • writing portable code (understanding the difference between standard guarantees and what is not guaranteed, but the implementation of your implementation), determined by the implementation, for example, the sizes of the main types).
  • C ++ standard libraries are limited compared to Java or PHP. You will also use custom libraries. For example, Maemo uses GTK + and / or Qt. Often the answer to the question "how can I do X in C ++": "you can not do this using only standard C ++, you need platform APIs or a portable library compiled for your system." X can be graphics, sockets, regular expressions, multithreading, XML processing, cryptography. Especially from mobile platforms you need to monitor OS versions, from time to time something can and will change.
+13
source

Learn how to use STL containers right away. Although the iterator syntax is not friendly for people coming from other languages, it provides built-in data structures that you usually use in Java or PHP collections as built-in (maps / hashes, lists, stacks, vectors) without typing fancy pointer code with dynamic allocation which so often distracts newcomers to reinvent the wheel and tinker with memory errors.

In addition, if you are writing platform-specific code (such as a Qt application or Microsoft MFC application), you will often see examples using containers specific to a specific infrastructure, where STL would be a wiser choice. STL (and Boost) can fill in the blanks. Using a graphical interface does not mean that you should use everything that the infrastructure offers. Stay away from non-standard containers unless you know, without a shadow of a doubt, that you will never port a program or reuse portions of code.

+3
source

I would say that this is not a variable type, since you need to clear the memory. Java will clear your memory, but C ++ will not do it for you. Otherwise, it is important to manage your resources when there are exceptions.

On the plus side, you get what they call "deterministic finalization." Great benefit. See how the acronym "RAII" is. I think this is perhaps one of the most important idioms in C ++.

This means that "Resource initialization is initialization," but it really means: "When this destructor fires, I will cleanse you after you, even if there are exceptions." In practice, this means that any object that you create or open to close or delete can be used with a smart pointer. The smart pointer will clear after you. It is very, very powerful, as soon as you understand it and start using it. This makes your error checking, exception handling and resource cleanup code very simple.

+2
source

One of them, which I saw, some Java programmers do when switching to C ++ is a try-catch memory leak. For instance:

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

In the above case, if the DoSomething () method throws an exception, pVar will never be deleted, so a memory leak will occur.

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

+2
source

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

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

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

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

(Note: leave const if you want to change the original my_array ).

+1
source

I am curious why here "PHP / Java" is seen as one thing. This is not true.

  • The transition to Java-> C ++ is quite large, but it is doable, you just need to learn a lot of additional syntax, a few additional concepts, such as destructors, copy constructors, splitting objects and operator overloading, and put up with the C ++ library.
  • On the other hand, the transition to PHP-> C ++ is another order of magnitude, since it refers to (a) a very poorly defined source language, (b) a language based on templates, not a class, and (c) the language, which runs in a special environment.
+1
source

Source: https://habr.com/ru/post/1305503/


All Articles