Is using too much static bad or good?

2019-04-04 14:24发布

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?

标签: c++ static
10条回答
放荡不羁爱自由
2楼-- · 2019-04-04 14:43

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.

查看更多
We Are One
3楼-- · 2019-04-04 14:45

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.

查看更多
一纸荒年 Trace。
4楼-- · 2019-04-04 14:50

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

查看更多
何必那么认真
5楼-- · 2019-04-04 14:51

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

查看更多
登录 后发表回答