Answer the question
In order to leave comments, you need to log in
Should I use private static functions in C++?
Or should I get away with C-style static functions? It's just that the latter doesn't seem very C++-style to me, in the sense that scope management is at the file level, not at the class level.
Answer the question
In order to leave comments, you need to log in
So and only so, because only in this case you will get access to the private and protected sections of the class
Actually the answer is ambiguous. And the criteria are important.
If we are talking about functions that implement something but do not require access to the private part of the class, then why declare them in the class, let them just lie in the cpp-shnik (if no one needs them outside)
It is important to understand that the less code the better, and the thinner the class interface, the better
if the functionality is performed. (within reasonable limits)
Therefore, extra code in the form of a function declaration in the header is bad.
However, if access is still needed (which is most likely the result of not the best decomposition), then of course yes
. This is not a matter of style. In C++, you can and should also write functions. It's not Java.
The correct answer is a combination of the two previous ones.
If you have one of the parameters - an object of this class and you need access to its private members, then this is the only way out. In other cases, for aesthetic reasons, a free function will look better.
some consider it good form that if a function does not work with class members, then it must be declared static. Some code analyzers may complain about this feature. If you think that this function should not be available from the outside, then it is quite possible to keep it private.
Some others, instead of such declarations in the cpp file in the unanimous namespace, simply write so as not to litter the header.
When using TDD, static functions are generally evil: D Since you just can't replace it to write a test.
So the answer is - you can, but if this is not practiced in the team, then no :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question