I have a function which aims to perform a recursive calculation. If my function is programmed recursively, it takes too long to compute. Therefore, I perform memoization by storing intermediate results in an array.
During the execution of my program, I might call the function with parameters (10,0)
,(5,5)
,(2,4)
etc. Therefore I have a setup(double x)
function which fills the entire array with the correct values. I can then access any of the array values without any further calculations. I only wait until x
changes to call setup()
again.
I am wondering how I can go about implementing this in c++. It doesn't make sense to me to use a class, as I would never need to create the associated object. I have implemented the functions fine in a namespace, but I'm still having a problem. Even If I use an unnamed namespace, the array used by my function is visible and can be modified from outside the namespace of the function. If I include the header file of the namespace, that is.
my code:
FunctionWrapper.h
namespace FunctionWrapper{
namespace{
double tempArray[10][10];
}
void setup(double x);
void getValues(int n);
}
Main.cpp
#include "FunctionWrapper.h"
int main(){
FunctionWrapper::tempArray[0][0] = 5; //Works
}