4

Regarding classes that only contain static members, I've seen multiple people claiming that it is a bad pattern and that it's never the best solution to a problem.

The accepted and top-voted answer on Are utility classes with nothing but static members an anti-pattern in C++? advocates using namespaces instead and concludes by stating

the most obvious answer to me is: Because we don't need OO to achieve this.

This is strange to me for several reasons. It seems like they are talking about classes only containing static member functions, and not classes containing a mix of static data and static functions. Classes with only static member functions can indeed be replaced with global functions inside a namespace, but how would you replace a class with containing both static functions and static data?

struct Foo
{
    static void add5()
    {
        s_x += 5;
    }
    static const int& getX()
    {
        return s_x;
    }
private:
    static int s_x;
};
int Foo::s_x{ 0 };

With functions and a global variable inside a namespace? How would you make sure s_x can't be accessed directly and modified by outside code?

The other thing that confuses me is that I feel like classes with only static members are a fairly common occurrence in books on C++ and in libraries.

SFML's sf::Mouse has only static members.

Game Programming Patterns uses classes with only static members several times, even referring to it with the term "static class" in the Singleton chapter. And the Service Locator chapter is inherently a class with only static members.

So how should one think? Are classes with only static members bad design / bad practice? Or do they have their place in some contexts?

5
  • 3
    The fact that you can find several examples of bad design doesn't make it good. :-) You can put your namespaced data in the cpp file and only expose the functions. You don't have to have it in the header.
    – BoP
    Commented Dec 14, 2021 at 23:10
  • 3
    I wouldn't make such a blanket "anti-pattern" statement. Like many aspects of C++, it can be misused or used well. While a namespace could be similar, your struct Foo is also a type which means it can be used in type contexts, such as template parameters. Commented Dec 14, 2021 at 23:12
  • I also wouldn't describe it as an "anti-pattern". However, there are plenty of articles around describing why it is often better - from a design or a coding perspective - to avoid statically allocated or "global" data - whether that is data in an unnamed namespace or static members of a class. But, like any feature of any programming language, there are circumstances where statically allocated data is a suitable or even preferable solution to a problem, and other circumstances where another option is better.
    – Peter
    Commented Dec 15, 2021 at 5:23
  • Related to namespace-functions-versus-static-methods-on-a-class.
    – Jarod42
    Commented Dec 15, 2021 at 11:14
  • ns3's Simulator class has only static members, too.
    – Yi Zhao
    Commented Jan 26 at 2:21

2 Answers 2

3

The top answer of the other question still holds: you do not need classes to encapsulate static functions and static variables. This can be done with namespaces and -- with a little discipline -- using the scope of compilation units.

In your example, you do not need to expose the existence of a private static data member if you're using namespaces:

Declare in a header:

namespace Foo {
    void add5();
    const int& getX();
}

Then implement in a cpp:

namespace Foo
{
    static int s_x{0};   // static globals are not visible outside the compilation unit

    void add5()
    {
        s_x += 5;
    }
    const int& getX()
    {
        return s_x;
    }
};

Be careful: static globals are not exactly the same thing than static members, but in the code above, it's the same effect as it's private. For public data members, you'd just remove the keyword static.

3
  • So for things that "static classes" are often used for, do you suggest ditching the class completely and using a namespace with static globals and global scope functions? Like for a game's Engine class or AudioSystem class, that there will only be one instance of and will be used in many places in the code. If you were writing a game, would you ditch these classes and put globals and functions in audio and engine-namespaces? Or would you write normal classes for these and then declare a global static instance of them in header files?
    – JensB
    Commented Dec 15, 2021 at 16:00
  • @JensB My message is not to get rig of classes at all cost, but to use classes only when it makes sense to have encapsulation (sse my comment about policy/strategy pattern on the other answer). C++ is multiparadigm, and if you have only one engine, and one audiosystem with a global state (because this is what static data members are), you have no advantage of using a class, and you'll have greater flexibility with namespaces (e.g. composing namespaces or selecting between different versions of it).
    – Christophe
    Commented Dec 15, 2021 at 16:39
  • @JensB I'm not in the game industry, so I can't help you on that. But on my shelf I have "Game Coding Complete" from Mike McShaffry. The book is really entertaining. And he knows games. So I opened it on the page about physic engine: he uses a real class with virtual functions and normal members. THis allows polymorphism and run-time configuration, reuse with game-specific specializations,a lot of things that you won't have withfake classes with only static members. Btw if you have functions that do not belong together, packaging them artificially in a class would go against SRP.
    – Christophe
    Commented Dec 15, 2021 at 16:55
3

While in general I recommend namespaces too, there are cases where the class is clearly a better approach. For example, you can pass a class as a template parameter, and that can mean you can have policy classes implement an interface differently, and "plug it in". You can't do that with namespaces.

But if you're not intending to refer to the group of names in such a way, I'd discourage it as a general approach to organizing code.

2
  • That's a valid point. There are indeed cases where a C++ class with only static members can make sense and the template is a very good example. It is however relevant mainly if there are several alternative classes (i.e strategy pattern, policy driven design). My answer is mainly for the general case of utility functions, especially if they are not really related. In this case namespace has even the advantage to facilitate versioning and namespace composition with using statements.
    – Christophe
    Commented Dec 15, 2021 at 0:41
  • @Christophe: One might say that "static classes" don't suffer from misusage of using namespace XXX; ;-)
    – Jarod42
    Commented Dec 15, 2021 at 11:11

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.