How to make an excellent C++ program without C traces?

Asked

Viewed 816 times

13

As a beginner in C++ I asked some questions here and was warned a few times by @Maniero that what I was doing was C and not C++. The problem is that C++ allows us to use many things similar to C.

So I ask: how to make a program "truly" C++ (without using C habits)?

1 answer

15


It is not easy to make an extensive list, I will try to put what is most important without worrying about small details (example: not having to use void to ensure that a function has no parameters).

Remembering that it is not forbidden to use, but it is good to get used to the specific parts of C++, which is even a language in mutation, so the best way to do it varies from version to version (which is at a fixed rate of 3 years) and don’t even recommend using some features that existed in C++ before the C++11 standard.

Some C things need to be used in very low-level code, when you do something more concrete, the standard C++ library itself is full of this, but it’s there so you don’t have to use it. If you are doing something like this it may be interesting or mandatory to use. Some rare cases the performance can only be obtained by doing the C-style, most are equal or faster to do in the C++ style (yes the C++ can be faster than C in many cases).

An implementation plan is under way for future versions (if not aborted) which prevents using problematic language resources, including those originating from C, with the possibility of releasing the specific access of each Feature in a well demarcated section.

Some things that are still done in C not even in C should do more, for example declare variable at the beginning of the function. I’m considering that people are already following the C99 style. And that they know how to do well, there are errors that I see around that is not even to use C in C+, is to do misuse even in C+, only in C++ the good use would be different.

C++ looks for more robustness and performance, so you have to understand every detail of the language to understand it. These are two languages that you can’t commit to half as you do in several others, at least not worth using them if you’re not 100% committed.

First of all the style of how write also changes, in general in C++ seeks more readability, in C seeks to be more succinct and less typing.

Some things here I’m speaking roughly.

  • The first thing you shouldn’t wear is library of C. Nothing should be used, and have a include with <algo.h> or <cNomeDoC> already indicates that you are doing wrong. If you consider this you can hardly do anything else in C, but you can still have a C style (plus).

  • Using almost anything of the preprocessor is wrong, mainly #define and macros and in part the #ifdef and other directives. Everything has better solution in C++, such as the use of const, enum, constrexpr, the inlining function (plus) and templates. Adding this already eliminates many other cases.

  • One of the most important points is to not use raw pointer and prefer references, smart pointers, besides not using array raw that decay to pointers and prefer library abstractions as array, vector, string, string_view, etc. These 3 points kill almost everything that is most important to delete C from your code.

  • Avoid Casts brute, has some templates ready to do it better.

  • Use Amble and its variations in place of function pointers.

  • Use the ability to Generics of templates in place of void * or other tricks (plus).

  • Forget to use namespaces is something bad (soon comes the modules).

  • Generally declare something like static in the sense of making a file private is no longer very suitable. The keyword still makes sense for another context.

  • Prefer for range with iterators or ready algorithms than for brute.

  • Functions have Overload, use them.

  • structs and classes are much more powerful, for example can have privacy in members, or functions can belong to them, until becoming methods.

  • More modern codes are preferring using in place of typedef.

  • C++ has auto, although rarely necessary in fact is more idiomatic, already decltype has more useful and necessary situations, which can eliminate certain tricks of PP.

  • Some cases what to put in a .hpp may be different from what you would put in a .h your (plus).

  • Remember that C++ is object-oriented, more functional, has other ways to produce the same result.

  • And in C++ there’s a lot of things ready that C doesn’t have, learn them, have a new way, simpler and robust, when it’s no longer performatic either.

These things are a good start.

It’s also not because there’s C++ that’s a good thing to use. There are cases that doing more C++ style can complicate interoperability with C if necessary, especially if C is the consumer (this is more rare).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.