I have a function which performs calculations and I would like to be able to call this function from anywhere in my program. I know that in Java, I would just create a public static method within a class.
So far in C++ I have created a namespace for my particular function. The problem I am having is that this function uses its own helper functions. I would like these lower level functions to be non-visible (i.e. private) but am not sure how to do so.
So far, I have this code:
namespace HelperCalc{
int factorial(int n){
return n <= 1 ? 1 : n*factorial(n-1);
}
double getProbability(int x, int y){
.....//do maths
.... = factorial(x);
}
}
So for example, I would to be able to call getProbability(), but I would like to ‘hide’ factorial().
Use an anonymous namespace (within a source file, not a header):