What is a “static” function in C?

前端 未结 12 1206
野的像风
野的像风 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:04

    First: It's generally a bad idea to include a .cpp file in another file - it leads to problems like this :-) The normal way is to create separate compilation units, and add a header file for the included file.

    Secondly:

    C++ has some confusing terminology here - I didn't know about it until pointed out in comments.

    a) static functions - inherited from C, and what you are talking about here. Outside any class. A static function means that it isn't visible outside the current compilation unit - so in your case a.obj has a copy and your other code has an independent copy. (Bloating the final executable with multiple copies of the code).

    b) static member function - what Object Orientation terms a static method. Lives inside a class. You call this with the class rather than through an object instance.

    These two different static function definitions are completely different. Be careful - here be dragons.

提交回复
热议问题