If you want to change the visibility of plain functions, you cannot use
public
,
protected
and
private
; these
keywords are specific to
class members.
To achieve your goal keep in mind the rules below:
- a
public
class method could be called from everyone, not only from inside another method of the class.
In a similar way, if you want your function to be accessible from other parts of your code, declare it inside an header file (i.e. *.h) and implement it inside a C/C++ code file (i.e. *.c or *.cpp).
This way, all modules that include your header file will be aware of your function and will be able to call it. - a
private
class method could be called only from inside another method of the class.
In a similar way, if you want your function to be hidden to modules other than the one where it is implemented, do not declare it on an header file. Instead declare it directly on the module where it is implemented, and declare it as static
.
This way, the function will be defined only in the scope of the module where it is implemented.
Here below is an example:
- inside example.h
int __cdecl MyGlobalFunction(int a, int b);
- inside example.cpp
#include "example.h"
static int MyPrivateFunction(int a, int b)
{
if (a > b) return a - b;
else return b - a;
}
int __cdecl MyGlobalFunction(int a, int b)
{
return MyPrivateFunction(abs(a), abs(b));
}