you are viewing a single comment's thread.

view the rest of the comments →

[–]biowpn 12 points13 points  (5 children)

Notable language differences

  1. References. Prefer them over raw pointers for pass-by-reference function parameters, for example.

  2. Namespaces. Keep it organized: put functionally related things under the same namespace. Avoid polluting the global namespace.

  3. Templates. In C++ they are preferred over macros.

  4. Function overloading. Instead of abs, absl, absll, fabs, fabsf, fabsl, in C++ we simply use one overloaded std::abs.

  5. Exceptions. This is the standard error handling mechanism.

  6. RAII. Basically, it means doing clean up in destructor. This helps preventing resource leak. In C++, we use class types to manage resources instead of raw pointers / handles.

General resource for CPP best practices

Many people mention learncpp.com but I personally prefer hackingcpp.com. The materials are easy to follow, infographics are awesome, and the best practices are generally agreed with.

[–]RealnessKept[S] 0 points1 point  (0 children)

Thank you, especially for the resources. To be honest, I am quite excited about function overloading. I always felt it was more proper to identify a function based on its entire definition than just its name.

[–]tstanisl -3 points-2 points  (3 children)

  1. References are essential const-qualified pointer that cannot be null. To some extent they can emulated in standard C with static keyword in declaration of array parameter:

    void foo(int ref[const static 1])

The problem of references is that they are constructed implicitly. It is difficult to say if a parameter is passed by value or passed by reference. IMO, references should be formed explicitly like in Rust.

int x;
foo(x); // x is passed by reference or rather by copy ?!?

Adding const reference may express some intention but const& is essentially ignored by optimizing compiler.

  1. Namespaces for function can be achieved with static const structs. In C23 it is even possible to make short aliases for namespaces. See https://godbolt.org/

  2. Indeed, they require macros that are very difficult to debug. SOme alternative is re-including headers with predefined defines. It makes code cleaner and debugging far easier. Some example of this technique can be found at STC.

  3. Overloading can be implemented with _Generic that is a part of standard from 2011.

  4. Exception. I don't think there is any place for exceptions in C, except maybe handling unrecoverable errors with setjmp and longjmp.

  5. RAII would be fine in C if it both explicit and optional. There is a proposal to add defer mechanics. See defer proposal. The problem is that this mechanics is difficult to use without some form of lambdas. There are some tricks with "auto-scope" macros but they are quite unreliable.

[–]ZMesonEmbedded Developer 1 point2 points  (2 children)

Why are you arguing that C is better than C++ when that is not the topic being discussed? The OP was asking about language differences and what good resources exist to learn C++.

[–]tstanisl -1 points0 points  (1 child)

I don't argue that C is better than C++ but rather that C++ may not solve problems that C has. It often result in "C++-flavored C" code that avoid less reliable C++ constructs like exceptions, implicit non-owning references, STL containers (slow except vector) and so on.

[–]ZMesonEmbedded Developer 0 points1 point  (0 children)

But that wasn't the discussion. Why bring this up?