What is a static function for?

10

What is a function with keyword static ? I know that declaring a local variable as static within a function will work as if it were a global variable, and a static function how does it work? And when should you use a static function?

I found that a function like static if inside a loop would only be called once, but this is not what happens in the example below:

static void MostrarMensagem()
{
    printf("Mostrando uma mensagem de uma funcao static!\n");
}

int main()
{
    while(1)
    {
        MostrarMensagem();
    }
    return 0;
}
    
asked by anonymous 01.12.2017 / 18:18

3 answers

8

Do you know languages classified as object-oriented? Do you know private ?

That is, when you put static in the function you are saying that it can only be accessed inside the source code file itself, it is private to this code and can not be called by other parts of the application. In C ++ it is not recommended to use.

It does not have the same Java or C # semantics. When used in class up it has similar but not identical behavior. The semantics are equal to C, the access restriction is in the file and not in the class. The only difference is that because it is in the class the full function name includes the class name.

And static is not a function as described in the question, it's just a scope modifier.

    
02.12.2017 / 15:03
2
  

What is a function with keyword static ?

static has different effects depending on its usage and context. It can be found in: function definition, global and local variables, data members, and member functions. As the question addresses the use of static in functions, the scope of the answer remains the same.

Note : I'm only considering to elaborate the answer. Some of the key concepts remain the same in (such as linking names), but I advise you to look for differences.

  

a static function how does it work?

To understand this, we need to know what link means in English) in .

A name that refers to an object, reference, function, type, template, namespace, value, or any other name found in [dcl.dcl] / declaration , you can have link (linkage being the English term). If a name has binding, then it refers to the same entity that the name introduces into a statement at some other scope. If a name (function, variable etc) is declared in multiple scopes but does not have a sufficient link between them, then multiple instances of the same entity are generated. There are some connection types, such as no link (in the linkage), internal linkage, and external linkage .

Second [basic.link] / 2 :

  • When a name has external link , the entity denoted by the name can be accessed in other Translation Units (read informally as other .cpp files). Functions declared in one UT and used in another is an example of such name (function name) and entity (function itself).
  • When a name has internal link , the entity denoted by the name can not be accessed by other UTs, but can be accessed by other scopes within the UT where the name was declared. Remember this in particular because this case is involved with the effect of static on a function declaration.
  • When a name has no connection, or no connection , the entity denoted by the name can not be accessed or out of the scope where it is declared. Local variables are an example, where more scopes can access it, but scopes do not.

If you continue reading to [basic.link] /3.1 , you will find the following: / p>

  
  • A name having namespace scope has internal linkage if it is the name of

         

    3.1 a variable, function or function template that is explicitly declared static ; or, [...]

  •   

    My emphases.

    Translating freely, we have that any name with a namespace scope will have internal binding if the name of a function variable, function, or template is explicitly declared with static .

    Finally, answering the question, a function declared with static causes its name to have an internal connection. That is, the function can only be accessed by its name within the UT in which it is declared (ie, informally, only within the .cpp file where it resides). Some effects of this are:

    • Ensures that the function can only be used within the UT where it resides;
    • Consequently, your name will not conflict with names of scopes outside UT;
    • As this has to do with binding, the linker (the final program that calculates the binary symbols and binds them) will have less work because the function name is not exported;
    • It gives more optimization possibilities for compilers, since the function can not be used outside UT, it can be fully optimized.

    We can see an example of the last two items here .

      

    When should you use a static function?

    One of its uses is to maintain the encapsulation of a feature that an UT provides, where the details of how functionality is implemented does not interest the user interface. Only the functions exposed for use would have external binding, while the implementation details remain internally linked.

        
    17.12.2017 / 19:56
    0

    Within a class definition, the static keyword declares members that are not bound to class instances.

    Outside the definition of a class, it has a different meaning: see storage duration .

    Reference:

    DevDocs - C ++ / static members . Available at: link . Accessed on: 12/01/2017.

        
    01.12.2017 / 18:47