mardi 2 août 2016

How do I persist a member variable across classes? Member function does not update

This is a noob question, I think. I have a class with a simple counter.

Class Foo {
  Public:
       void incrementCount();
       int getCount();
       Foo(int count) : m_count(count) {};
  Private:
       int m_count;
}

int getCount() {
     return m_count;
}

void Foo::incrementCount() {
     m_count = m_count + 1;
}

This class is used as a map in another class. In one function, I am iterating through this map and updating the count.

typedef std::map<std::string, Foo> fooList;

fooList::iterator got = foo_list.find("bar");

if (got == foo_list.end()) {

     Foo foo(1);
     std::pair<std::string, Foo> bar("bar",foo);
     foo_list.insert(bar);

} else {

     got->second.incrementCount();

}

This is successful. I can see the count is updated.

But in a separate class, I am trying to retrieve the updated count and instead I am receiving the initialized value. Am I missing something obvious?

Aucun commentaire:

Enregistrer un commentaire