Advertisement

Static vector looses data

Started by December 07, 2000 11:31 PM
0 comments, last by gimp 24 years, 1 month ago
I have a problem with a static vector in my class and I''m not sure if I''m being too tricky for myself or not... Imagine something like this(typed quickly dont be picky, my version compiles)
  
class CExecuteObject
{
    add()
    virtual Execute()=0;
    static ExecuteAll()

    static vector<CExecutionObject*> Database;
}
  
- add() will just .push_back(this); - Executeall() will iterate through each item and call .execute() on it. Now the problem is that when I derive a few classes from this and call add() a few times the pointers don''t seem to stay in the static vector, each new item added see''s an empty vector. Of course ExecuteAll see''s nothing in the vector at all All suggestions welcome Thanks Chris
Chris Brodie
What you have seems fine for a base class. You might want to show how you''re deriving. If you declare the static method and static vector in derived classes as well as in the base class, they will override the base class and it will cause you to have multiple statics running around.

In other words, make sure that the base class is the ONLY place you declare static void ExecuteAll () and static vector&ltCExecutionObject*> Database.

You might want to look at putting some debug code into the add function. Maybe print out the new size of Database after each add, and print the address of Database to see if you have multiple Database objects lying around.

This topic is closed to new replies.

Advertisement