Global class member?

Hey,

I'm new to the forums, and got a little question =].

Let's say that I've got a class wich adds an item to a <vector> ,
and when I press for example the button "Q", an item is deleted from the <vector> .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Pseudocode
  class obj{
vector<item> items;
public:
...
void addItem(item i){
items.push_back(i);
}
void removeItem(){
if(KeyboardKeyPressed->"Q")
items.pop_back();
}
...
};


But now let's say i create multiple objects with the class obj like this;
 
obj a, b;


Pressing Q would delete 2 items together, because the 2 objects i created have their own vectors.
Is there any way to create an global vector for any object that i create from the class obj?

Thanks in advance!
you could make the vector static. then it will be shared across all instances of a class. of course, there are some alternatives:

1
2
3
4
5
void removeItem(char sentinel) {
    if(KeyboardKeyPressed->sentinel) {
        items.pop_back();
    }
}


or what i recommend doing: test if the keyboard was pressed in a function you call, then if it was, have that function call obj::removeItem()
Thanks for the fast reply.

Tried the static vector way, but now it does not let me modify the vector.

As for the recommended way, calling the function would again delete one item from obj a, and one item from obj b.
Why would b change is you only call a.removeItem()?
Sorry, I did not pointed out, but in my program, it's not actually a function call.
By default the object is checking if "Q" is pressed. It means on every object instance, they are all checking if "Q" is pressed.

EDIT:
I made it like a function, and now if i call if on only one object, it removes one by one from the last one in the vector. Exactly what i needed actually.
It's not a prefect solution but it would do the job. Thanks =]
Last edited on
Topic archived. No new replies allowed.