What is a “static” function in C?

前端 未结 12 1204
野的像风
野的像风 2020-11-22 16:03

The question was about plain c functions, not c++ static methods, as clarified in comments.

I understand what a static variable is, but wha

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 16:12

    A static function is one that can be called on the class itself, as opposed to an instance of the class.

    For example a non-static would be:

    Person* tom = new Person();
    tom->setName("Tom");
    

    This method works on an instance of the class, not the class itself. However you can have a static method that can work without having an instance. This is sometimes used in the Factory pattern:

    Person* tom = Person::createNewPerson();
    

提交回复
热议问题