Static member functions

后端 未结 5 1272
自闭症患者
自闭症患者 2020-12-08 16:30

After reading sbi and Eli Bendersky\'s answers in this question I started to wondering what static member functions are for.

A class\' friend free function shouldn\'

5条回答
  •  囚心锁ツ
    2020-12-08 17:11

    Static methods:

    • Provide encapsulation in the "namespace" created by the class. If your class is Animal and the static method is Create, you have to call it with Animal::Create. This is better than global functions, and allow implementing Factories and "virtual constructors" with relatively natural syntax.
    • Have access to static members. Such members are useful in some cases, and without static methods and members you would have to use global variables and functions.

提交回复
热议问题