Tanmay Bhatnagar
Tanmay Bhatnagar

Reputation: 2470

What is the use of static member functions that are not related / declared inside a class?

I totally understand the use of static functions that are members of a class. But what might be the probable use of static functions that are not related to any class or defined globally. eg-

#include <iostream>
using namespace std;
static int func()
{
    cout<<"This is a function";
}

int main()
{
 /*Random code here*/
return 0;
}

Upvotes: 1

Views: 64

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490138

This creates a function that's visible only inside that translation unit. A translation unit is basically that source file (after preprocessing, so what was in the headers it included plus what's directly in the file itself).

It's roughly equivalent to putting the function inside an anonymous namespace, but the anonymous namespace is generally considered preferable.

Upvotes: 4

Related Questions