<p>I like to use static functions in C++ as a way to categorize them, like C# does.</p>

<pre><code>Console::WriteLine("hello")
</code></pre>

<p>Is this good or bad? If the functions are used often I guess it doesn't matter, but if not do they put pressure on memory?</p>

<p>What about <code>static const</code>?</p>

[{'answer_id': 31004, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 3, 'selected': False, 'text': "<p>I'm all for using static <strong>functions</strong>. These just make sense especially when organized into modules (<code>static class</code> in C#).</p>\n\n<p>However, <em>the moment</em> those functions need some kind of external (non compile-time const) data, then that function should be made an instance method and encapsulated along with its data into a class.</p>\n\n<p>In a nutshell: static functions ok, static data bad.</p>\n"}, {'answer_id': 31013, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 1, 'selected': False, 'text': '<p>Agree with Frank here, there\'s not a problem with static (global) functions (of course providing they are organised).. The problems only start to really creep in when people think "oh I will just make the scope on this bit of data a little wider".. Slippery slope :)</p>\n\n<p>To put it really into perspective.. <a href="http://en.wikipedia.org/wiki/Functional_programming" rel="nofollow noreferrer">Functional Programming</a> ;)</p>\n'}, {'answer_id': 31033, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 1, 'selected': False, 'text': '<p>One specific reason static data is bad, is that C++ makes no guarantees about initialization order of static objects in different translation units. In practice this can cause problems when one object depends on another in a different translation unit. Scott Meyers discusses this in Item 26 of his book More Effective C++.</p>\n'}, {'answer_id': 31094, 'author': 'Kristopher Johnson', 'author_id': 1175, 'author_profile': 'https://Stackoverflow.com/users/1175', 'pm_score': 1, 'selected': False, 'text': '<p>I tend to make classes that consist of static functions, but some say the "right way" to do this is usually to use namespaces instead.  (I developed my habits before C++ had namespaces.)</p>\n\n<p>BTW, if you have a class that consists only of static data and functions, you should declare the constructor to be private, so nobody tries to instantiate it.  (This is one of the reasons some argue to use namespaces rather than classes.)</p>\n'}, {'answer_id': 31111, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n  <p>but is it good or bad </p>\n</blockquote>\n\n<p>The first adjective that comes to mind is "unnecessary".  C++ has free functions and namespaces, so why would you need to make them static functions in a class?</p>\n\n<p>The use of static methods in uninstantiable classes in C# and Java <em>is a workaround</em> because those languages don\'t have free functions (that is, functions that reside directly in the namespace, rather than as part of a class).  C++ doesn\'t have that flaw.  Just use a namespace.</p>\n'}, {'answer_id': 31119, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 1, 'selected': False, 'text': "<p>The problem with static functions is that they can lead to a design that breaks encapsulation.  For example, if you find yourself writing something like:</p>\n\n<pre><code>public class TotalManager\n{\n    public double getTotal(Hamburger burger)\n    {\n        return burger.getPrice() + burget.getTax();\n    }\n}\n</code></pre>\n\n<p>...then you might need to rethink your design.  Static functions often require you to use setters and getters which clutter a Class's API and makes things more complicated in general.  In my example, it might be better to remove Hamburger's getters and just move the getTotal() class into Hamburger itself.</p>\n"}, {'answer_id': 31213, 'author': 'Johannes Hoff', 'author_id': 3102, 'author_profile': 'https://Stackoverflow.com/users/3102', 'pm_score': 2, 'selected': False, 'text': '<p>Use namespaces to make a collection of functions:</p>\n\n<pre><code>namespace Console {\n    void WriteLine(...) // ...\n}\n</code></pre>\n\n<p>As for memory, functions use the same amount outside a function, as a static member function or in a namespace. That is: no memory other that the code itself.</p>\n'}, {'answer_id': 31272, 'author': 'KannoN', 'author_id': 2897, 'author_profile': 'https://Stackoverflow.com/users/2897', 'pm_score': 0, 'selected': False, 'text': '<p>For organization, use namespaces as already stated. </p>\n\n<p>For global data I like to use the <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow noreferrer">singleton</a> pattern because it helps with the problem of the unknown initialization order of static objects.   In other words, if you use the object as a singleton it is guaranteed to be initialized when its used.</p>\n\n<p>Also be sure that your static functions are stateless so that they are thread safe.</p>\n'}, {'answer_id': 3124821, 'author': 'Erius', 'author_id': 377052, 'author_profile': 'https://Stackoverflow.com/users/377052', 'pm_score': 0, 'selected': False, 'text': "<p>I usually only use statics in conjunction with the friend system.</p>\n\n<p>For example, I have a class which uses a lot of (inlined) internal helper functions to calculate stuff, including operations on private data.</p>\n\n<p>This, of course, increases the number of functions the class interface has.\nTo get rid of that, I declare a helper class in the original classes .cpp file (and thus unseen to the outside world), make it a friend of the original class, and then move the old helper functions into static (inline) member functions of the helper class, passing the old class per reference in addition to the old parameters.</p>\n\n<p>This keeps the interface slim and doesn't require a big listing of free friend functions.\nInlining also works nicely, so I'm not completely against static.\n(I avoid it as much as I can, but using it like this, I like to do.)</p>\n"}, {'answer_id': 10656010, 'author': 'Andrew', 'author_id': 1403796, 'author_profile': 'https://Stackoverflow.com/users/1403796', 'pm_score': 2, 'selected': False, 'text': '<p>Those who say static functions can be replaced by namespaces are wrong, here is a simple example:</p>\n\n<pre><code>class X\n{\n   public:\n   static void f1 ()\n   {\n      ...\n      f2 ();\n   }\n\n   private:\n     static void f2 () {}\n};\n</code></pre>\n\n<p>As you can see, public static function <code>f1</code> calls another static, but private function <code>f2</code>.</p>\n\n<p>This is not just a collection of functions, but a smart collection with its own encapsulated methods. Namespaces would not give us this functionality.</p>\n\n<p>Many people use the "singleton" pattern, just because it is a common practice, but in many cases you need a class with several static methods and just one static data member. In this case there is no need for a singleton at all. Also calling the method <code>instance()</code> is slower than just accessing the static functions/members directly.</p>\n'}]
['https://Stackoverflow.com/questions/30998', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2566/']