4

A very straightforward question.

When I think about many of the member functions I create for my classes, many of them can be made static without affecting any functionality whatsoever.

If I do so; are there any practical benefits [or drawbacks] I can expect in terms of memory or performance?

2

1 Answer 1

10

Practically in 99.9% of cases: no.

Theoretically: maybe. You won't be passing the implicit this parameter to every function, and not passing that could save you bytes and the time passing those bytes. The compiler may well optimise it away though in any case. If profiling has shown one of these functions is on your hot path, it might be worth considering.

However, I'd argue you should still do this anyway, because it makes it clear to anybody looking at the code that your static function doesn't act on any object state. That's far more important than any discussion about memory or performance.

1
  • 1
    I would be a bit careful. Say you have a button which has a size, a title, a font for the title, and a colour. And the colour is always completely determined by your GUI. I wouldn’t make it a static function just because I can, but make sure that it is accessed the same way as the others.
    – gnasher729
    Commented Mar 12, 2021 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.