Thread: are static class members the right choice?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    are static class members the right choice?

    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.
    Last edited by drrngrvy; 10-11-2005 at 06:53 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well im guessing here that your strings have gone out of scope and you are left with an array of dangling pointers. I'd suggest converting to std::vector for the array and std::string for all the char* then the code will be simpler, more readable and less bug-prone.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    how exactly is this CGI called? Is it running within a process or is it compiled as an executable and just executed on each call? Most of the time when you do a CGI in C/C++, they are run new each time, so you can't have variables stored between instances of the processes. In this case, you will need to store the data to a file on start of the call and write to the file when finished.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    oh, sorry, didn't read the 2nd post, he's write you will probably want to change it to std::string because the char* will be compared by address, not by the text string. Another tip, when you do:

    Code:
    params[p] != NULL
    that's not a valid way to find in a map, at least not to my understanding, maybe I'm wrong here, but the way that I was taught to do it was:

    Code:
    if (params.find(p) == params.end())

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the feedback guys. I wasn't trying to get the variable to be visible between instances; I couldn't even get it visible on a single free-space instance. Ha!

    As it turns out, making the function param() return a string type instead of char *, coupled with changing the map<char *, char *> to a map<string, string> solved the problem. It's a pity because for some reason I prefer char *s to the string type. Plus, just those four or five changes add 30k to my executable.

    And rockytriton, thanks for that little 'null string' snippet. I was just wondering how to do that. Heh.

    Now I have to find out exactly why the string type is so important for the map<>...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It was mentioned earlier. If you save a char pointer in the map, a copy of that pointer is stored. So what that pointer points to must be valid as long as the map is alive. On the other hand, if you save a string in the map, a copy of the string is stored, and so it won't go out of scope since the map has its own copy of the data.

    Also, maps compare key values in order to keep the data in sorted order. If you use string, the string value will be compared (two strings will match if they have the same string value). If you hold char*, then the pointer value will be compared, so even if two char*s point at the same string value, they won't match if that value is in two different memory locations.

    You can use char* in a map, but it takes a lot of effort. You have to manage memory to make sure stored pointers are always valid, and you have to write your own comparison to get them to copare correctly. The ease of use and robustness of the string method is why they are usually worth the 30k addition to your executable.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Excellent reply Daved. That's the sort of info I wouldn't have ever discovered 'by accident' (or on purpose, if I was being honest). Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  5. namespaces and static class members
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2003, 08:55 PM