E
E
e_asphyx2014-10-29 09:26:40
C++ / C#
e_asphyx, 2014-10-29 09:26:40

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

5 answer(s)
D
DancingOnWater, 2014-10-29
@DancingOnWater

So and only so, because only in this case you will get access to the private and protected sections of the class

K
Koss1024, 2014-10-29
@Koss1024

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.

D
degs, 2014-10-29
@degs

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.

T
Trrrrr, 2014-10-30
@Trrrrr

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 :)

D
Don Kaban, 2014-11-01
@donkaban

www.etextlib.ru/Book/Details/2972

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question