Is using too much static bad or good?
Asked Answered
D

11

12

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

Console::WriteLine("hello")

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?

What about static const?

Deglutinate answered 27/8, 2008 at 19:36 Comment(1)
Cant you just use namespaces?Vestpocket
P
28

but is it good or bad

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?

The use of static methods in uninstantiable classes in C# and Java is a workaround 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.

Pibroch answered 27/8, 2008 at 20:10 Comment(1)
I agree. +1. "static" methods could have their use, but I'm unable to provide one, or to find one in all the posts, that is not already covered by namespaces. GO NAMESPACES. :-)Cower
D
10

I'm all for using static functions. These just make sense especially when organized into modules (static class in C#).

However, the moment 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.

In a nutshell: static functions ok, static data bad.

Darees answered 27/8, 2008 at 19:38 Comment(0)
L
4

Those who say static functions can be replaced by namespaces are wrong, here is a simple example:

class X
{
   public:
   static void f1 ()
   {
      ...
      f2 ();
   }

   private:
     static void f2 () {}
};

As you can see, public static function f1 calls another static, but private function f2.

This is not just a collection of functions, but a smart collection with its own encapsulated methods. Namespaces would not give us this functionality.

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 instance() is slower than just accessing the static functions/members directly.

Lesson answered 18/5, 2012 at 16:3 Comment(1)
As long as there is a source (.c/.cpp) file, "private" static functions can still exist within a namespace similar to your example. Only define the private function (f2()) inside the source file and mark it as static (in this case it will only be accessible to the single file/translation unit). You'll be able to call f2() from f1() as long as its implemented in the same file. With the added benefit of not requiring a function prototype in the header! You could do the same with static data too, just define it in the source file! However, this is a very "C-style" way of organising code...Gospel
C
2

Use namespaces to make a collection of functions:

namespace Console {
    void WriteLine(...) // ...
}

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.

Celebrity answered 27/8, 2008 at 20:38 Comment(0)
P
1

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

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

Pollie answered 27/8, 2008 at 19:36 Comment(0)
W
1

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

To put it really into perspective.. Functional Programming ;)

Wes answered 27/8, 2008 at 19:40 Comment(0)
L
1

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++.

Lavellelaven answered 27/8, 2008 at 19:45 Comment(0)
S
1

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:

public class TotalManager
{
    public double getTotal(Hamburger burger)
    {
        return burger.getPrice() + burget.getTax();
    }
}

...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.

Superpower answered 27/8, 2008 at 20:12 Comment(0)
A
0

For organization, use namespaces as already stated.

For global data I like to use the singleton 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.

Also be sure that your static functions are stateless so that they are thread safe.

Abroms answered 27/8, 2008 at 20:53 Comment(0)
T
0

I usually only use statics in conjunction with the friend system.

For example, I have a class which uses a lot of (inlined) internal helper functions to calculate stuff, including operations on private data.

This, of course, increases the number of functions the class interface has. To 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.

This keeps the interface slim and doesn't require a big listing of free friend functions. Inlining also works nicely, so I'm not completely against static. (I avoid it as much as I can, but using it like this, I like to do.)

Trapan answered 26/6, 2010 at 17:31 Comment(0)
I
-1

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?

The use of static methods in uninstantiable classes in C# and Java is a workaround 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.

Inboard answered 14/5 at 11:13 Comment(1)
This is just the accepted answer without the first line. This adds nothing and it's not clear what you hope to achieve. Please don't do this.Solvable

© 2022 - 2024 — McMap. All rights reserved.