>What do you mean about me modifying a string literal though? Where do I do that?
In your "test" example:
Code:
c.save_param("bill=bob");
This sends a string literal to CLASS::save_param. Then in save_param you do this:
Code:
char *v = strchr( _nvpair, '=' );
v still points to a string literal at this point. Then shortly thereafter you do this:
Which is modifying the contents of a string literal, which is bad because string literals can be stored in read-only memory. That breaks your example program, and once you fix it everything works fine:
Code:
#include <iostream>
#include <map>
#include <string>
class CLASS
{
public:
static std::map<std::string, std::string> params;
void save_param( const char *_nvpair );
};
std::map<std::string, std::string> CLASS::params;
void CLASS::save_param( const char *_nvpair )
{
const char *n = _nvpair;
const char *v = strchr( _nvpair, '=' ); // this points just before 'value' begins
if( v != 0 ){
std::string name(n, v - n);
std::string value(v + 1);
params[name] = value; // assign to the map<>
}
}
int main(void)
{
CLASS c;
c.save_param("bill=bob");
std::cout<< "bill = " << c.params["bill"] << std::endl;
return 0;
}