C++ static local function vs global function

后端 未结 4 1599
一整个雨季
一整个雨季 2021-01-31 08:13

What is the utility of having static functions in a file ?

How are they different from having global functions in a file ?

static int Square(int i)
{
            


        
4条回答
  •  春和景丽
    2021-01-31 09:00

    What is the utility of having static functions in a file?

    You can use these functions to provide shared implementation logic to other functions within the same file. Various helper functions specific to a file are good candidates to be declared file-static.

    How are they different from having global functions in a file?

    They are invisible to the linker, allowing other compilation units to define functions with the same signature. Using namespaces alleviates this problem to a large degree, but file-static functions predate namespaces, because they are a feature inherited from the C programming language.

提交回复
热议问题