Going from C to C++

Instead of “typedef” use “using”

From “https://www.nextptr.com/tutorial/ta1193988140/how-cplusplus-using-or-aliasdeclaration-is-better-than-typedef” :


1
2
3
4
5
<span class="com">// create an alias of reference to int[4];</span>
<span class="kwd">using</span> <span class="typ">IntArray4</span> <span class="pun">=</span> <span class="kwd">int</span><span class="pun">(&amp;)[</span><span class="lit">4</span><span class="pun">];</span>

<span class="com">// or with typedef</span>
<span class="com">// typedef int(&amp;IntArray4)[4];</span>

When it comes to defining simpler type aliases, choosing between typedef and alias-declaration could be a matter of personal choice. However, while defining the more complex template aliases, function-pointer aliases, and array reference aliases, the alias-declaration is a clear winner.

Instead of “#define” use “const”

From : https://root.cern.ch/TaligentDocs/TaligentOnline/DocumentRoot/1.0/Docs/books/WM/WM_92.html

Never use #define symbolic constants Instead, use the C++ const storage class. As with #define symbols, const declarations are evaluated at compile time (for types and expressions that qualify as compile-time constants). Unlike #define symbols, they follow the C scope rules and have types associated with them. You can also use enum to prevent a host of problems. For example:

  #define kGreen 1                    // Bad
  const int kGreen = 1;               // Better
  enum Color {kRed, kGreen, kBlue}    // Best

If you accidentally redefine a name with a #define, the compiler silently changes the meaning of your program. With const or enum you get an error message. Even better, with enum you can put the identifiers in the scope of a class. As an added bonus, each enumeration is treated as a separate type for purposes of type checking (much like the way scalars are handled in Pascal) and for purposes of overloading.

Unlike in ANSI C, objects in C++ that are declared const and initialized with compile-time expressions are themselves compile-time constants (but only if they are of integral or enumeration type). Thus, they can be used as case labels or in compile-time expressions.

Use auto whenever possible

https://subscription.packtpub.com/book/application_development/9781786465184/1/ch01lvl1sec5/using-auto-whenever-possible

Use C++ cast instead of C casting

int f = (int) test; vs static_cast<int>(test) –  See http://www.cplusplus.com/doc/oldtutorial/typecasting/

Use std::string instead of char array

1
std::string

 instead of character arrays

Use the Standard Template Library containers

Migrate from hand-crafted containers to C++ Standard Template Library containers

Use Nullptr instead of Null

From Stackoverfolow : Enter nullptr. In C++11, nullptr is a new keyword that can (and should!) be used to represent NULL pointers; in other words, wherever you were writing NULL before, you should use nullptr instead. It’s no more clear to you, the programmer, (everyone knows what NULL means), but it’s more explicit to the compiler, which will no longer see 0s everywhere being used to have special meaning when used as a pointer.

Use Namespaces

Here is a blog about why ;https://cppdepend.com/blog/?p=79


1
2
3
4
<var>namespace</var> myNamespace
{
  <var>int</var> a, b;
}

 

Use reference instead of pointer for parameters to functions

Reference can’t be NULL, so you don’t be accident de-reference a NULL pointer.

 

Use ++i instead of i++

You should prefer pre-increment operator (++iter) to post-increment operator (iter++) if you are not going to use the old value. Avoids copy of value

Migrate from structs to classes.

Use Smart pointers

 

  1. Better Initialization

  2. Scoped Enumerations

  3. Use range-based loops
  4. Add const to end of method declaration if no member variable are/must be changed + mutable
  5. Use enum class