Public Group

# Sharing a static member to derived objects

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Here is my situation, I have a base class called State and several derived classes, such as Running and Placing. The master game object maintains an array of these derived state objects. I use the common methods to move my game along. Most of the derived classes need access to a vector that is contained inside the main game object. This is a vector of all in game characters, which I call "units". The easy solution is to pass a reference to the units vector to each derived class that needs it when I create them. What I would like to do is make the reference a static member of the base class and let all derived classes access it through that. That seems very clean and the "right" way to do things. To bad I cant seem to get the syntax right. Here is the relevant code: class State{ protected: // Reference to "global" list of units static vector<Unit*>& unitsI; public: static void setUnitList(vector<Unit*>& unitsIn) {unitsI = unitsIn;} } And then in the setup process in my game object I do something like this: vector<Unit*> units; State::setUnitList(units); When I try to compile it I get: undefined reference to State::unitsI So I add this to the end of the class's .h file: vector<Unit*>& State::unitsI; Except now it complains: 'State::unitsI' declared as reference but not initialized I could create a temp vector, assign it to the reference and then reassign the reference later but that makes the compiler shout a lot about "multiple definitions", so that seems like the way to go. I could use a pointer to the vector instead of a reference like this: vector<Unit*>* unitsI; but that gives me the same "multiple definition of State::unitsI" error message that I get when I try the temp vector. What is a good way to do this? Am I missing some trivial trick or is my idea flawed? -Waxler

##### Share on other sites
Quote:
 I could create a temp vector, assign it to the reference and then reassign the reference later

No, you can't. References must be set when they are created, and cannot be retargeted later.

Use a pointer. There even is a special pointer value for pointers that don't point to anything: 0 .

##### Share on other sites
vector<Unit*>* State::unitsI;

must be in a cpp file

##### Share on other sites
The cpp file bit was the trick.

I would rather use a reference than a pointer, just because I have written a bunch of states that all refer to it as a reference.

Thanks.

##### Share on other sites
You cannot dynamically change references, because it would be like trying to dynamically change the address of a variable, but you can dynamically change pointers, since they're meant to be changed.
However, you can do this the tedious way:
class State{protected:// Reference to "global" list of unitsstatic vector<Unit*> *ptrUnitsI;public:static void setUnitList(vector<Unit*> &unitsIn) {*unitsI = unitsIn;}};//And if you've written the other functions using 'unitsI' as a reference, you can recreate it in each function:TYPE State::something(ARGUMENTS) {  vector<Unit*> &unitsI=*ptrUnitsI; //Someting like that  ...}

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631068
• Total Posts
2997740
×