Hey there,
I'm having real trouble finding an answer to this problem.
Basically I have a class CGI, which parses and saves user variables as a map<> called 'params'. Saving them is fine, but when I want to access them (via a member function rather than directly) they are empty.
I thought a setting 'params' as a static member would solve it, but it hasn't worked so far. From what I gather static is more for different instances of the class to access it, whereas I have a single instance (created via new) so maybe I'm trying the wrong thing? Either way, declaring them static alone doesn't make a difference.
Anyway here's a snippet:
Code:
class CGI
{
private:
static std::map<char *, char *> params;
...
void save_params(char *); // defined elsewhere
public:
CGI();
char *param(char *); // defined elsewhere
...
};
std::map<char *, char *> CGI::params;
char * CGI::param( char *p )
{
std::cout<< p << " and " << std::flush;
if( params[p] != NULL )
return params[p];
else
return "no such variable";
}
void CGI::save_param( char *nvpair )
{
char *n = nvpair;
char *v = strchr( nvpair, '=' );
*v = 0x00; // seperates n from v
++v; // now v points to the value
params[n] = v;
std::cout<< n << " = " << params[n]; // this works
}
The thing is that I have tested that the 'params' are saved by calling them at the end of save_params. They exist there, but by the time I call CGI::Param() the variables are NULL.
I've tried static functions too, but not only are they ugly (can't be declared at file scope) but they've not helped. I thought that I might have had to save it or display it with a static function, but I don't know. Any ideas?
I'd appreciate any help you have. I'm completely stuck.