mrlachatte: (Default)
[personal profile] mrlachatte
Riddle me this.  I've got defitinitions like this:

--------------

class State
{
    private:
       GUI *gui;
       std::map<char *, int> iVars;

    public:
       State()
       {
          set_ivar("test", 1);
          gui = new GUI(this);
       }

       void set_ivar(char *name, int value)
       {
          iVars[name] = value;
       }

       int get_ivar(char *name)
       {
          if(iVars[name])
             return iVars[name];
          else return 0;
       }
};

class GUI
{
    public:
       GUI(State *s)
       {
          printf("value is %d", s->get_ivar("test"));
       }
};

----------

Now, ignoring the nice circular dependency going on there, explain why I would get a 0 in the printf() in the GUI constructor.  The real problematic code is more complex, but I've tested, and the values in the map<> are valid before the call to create the GUI.  However, inside the GUI constructor, suddenly they don't exist anymore.  Help :(

Date: 2006-06-12 01:50 am (UTC)
From: [identity profile] kvance.livejournal.com
Here's a theory off the top of my head: std::map doesn't do anything special for the char* data type, which you want to use as a string. You're really just mapping a pointer to an int.

The string "test" in State::State() is in a different memory location than the string "test" in GUI::GUI(). Since that pointer isn't in the map, you get the zero return value. If you had used a std::string as the key type, the strings' VALUES would have been compared and your int would have been found.

I could, of course, be wrong.

Date: 2006-06-12 03:08 am (UTC)
From: [identity profile] inmatarian.livejournal.com
Wow, shows what I know. I looked at it for a few minutes and couldn't figure it out. F'ing Lua messing with my understanding of associative containers.

An alternate fix would be to have a const char * str_test = "test"; in a common scope between them and use the str_test identifier.

Date: 2006-06-12 03:22 am (UTC)
From: [identity profile] mrlachatte.livejournal.com
Oh man, I'm feeling pretty dumb right now. Thanks all of you.

Date: 2006-06-12 02:23 am (UTC)
From: [identity profile] highlyeccentric.livejournal.com
o josh, i'm begining to fear for your sanity...

December 2007

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526 272829
3031     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 11th, 2025 05:01 am
Powered by Dreamwidth Studios