Turning a global function into a file-local function

Turning a global function into a file-local function

In standard C, a function declared static has an internal linkage; it is accessible only from within the translation unit (source file) in which it is declared, but not from anywhere else. This technique is used to support information hiding as in the following case:

 //File hidden.cstatic void decipher(FILE *f);//accessible only from within this file//now use this function in the current source filedecipher ("passwords.bin");//end of file 

Though still supported in C++, this convention is now considered deprecated, so future releases of your C++ compiler may issue a warning message when finding a static function which is not a member of a class. In order to achieve the same effect in C++ (i.e., when migrating legacy C code), you should use a nameless namespace instead:

 //File hidden.cppnamespace { //namelessvoid decipher(FILE *f); //accessible only from within this file}//now use this function in the current source file. No need for any //'using' declarations or directives.decipher ("passwords.bin");//end of file 

It is guaranteed that nameless namespaces in different source files are unique, so if you declare yet another decipher(FILE*) function within a nameless namespace in another file, the two decipher functions are hidden from each other and hence, do not collide.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several