Thread: are static class members the right choice?

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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