In a member function of my class I made a string array. In a different member function (same class) I need to be able to read that array. I did a quick search here and didn't find anything, google was hinting that I needed to use a pointer??
In a member function of my class I made a string array. In a different member function (same class) I need to be able to read that array. I did a quick search here and didn't find anything, google was hinting that I needed to use a pointer??
Technically, you can return an array from a function, but 99% of the cases where this is asked, the CORRECT thing to do is to pass in an array, modify that array, rather than returning an array, so unless you have a VERY specific reason to return an array from a function, that would be my suggestion. The reason for this suggestion is that most of the time someone wants to return an array, that is returning a local variable array - which makes no little sense.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Well I am kinda brainstorming this whole thing so I will just post the parts of the code I have....I know its messy and prolly wont compile but I am just trying to get it down on paper before I hit the compiler...
This is the class
This is the member function I need the array for. CastSpell() needs to be able to search through the array to find the list of mobAbilities.Code:class Mobs { public: string getMobState(); // random roll to decide if its caster or melee void getMobColor(); // random roll to get mobs color (difficulty) void getMobName(); // caster name if its a caster or a melee name if its melee int getStats(); // stats refer to hit points and mana string getAbilities(); // string array ?? runMobScript(); // not sure what the return type should be...will decide what spells are cast void CastSpell(); // code for castig spell, returns text about what happened int meleeDamage; // how hard the mob physically hits private: int mobMaxHP; // mobs hitpoints int mobMaxMP; // mobs mana string mobLocation(); // the mobs location sting mobState; // caster or melee? string mobName; // Sets mobs name string mobAbilities[]; // will be an array of strings }
And the member function where I need to return the arrayCode:?? Mobs::runMobScript() { If (mobName == “Troll Magician”) { CastSpell(); // check to see if mob has mana, pick a random spell from abilities list and cast if Mob has the mana to cast it. If mob has no mana, melee hit
Code:string Mobs::getAbilities() { if (mobState == caster && mobLocation == "troll bane cave") { “Frostbolt”, “LighteningBolt”, “Firebolt” // Place these spells in a string array?? } If (mobState == melee && mobLocation == “troll bane cave”){ “Sunder”, “Fire Blade”, “Ice Blade”, “Lightening Blade” // place these abilities in a string array?? } if (mobState == caster && mobLocation == "Orc Haven") { “Frostbolt”, “LighteningBolt”, “Firebolt”, “FlameWalker”, “FlameWalker Nova” // Place these spells in a string array?? } If (mobState == melee && mobLocation == “Orc Haven”){ “Sunder”, “Fire Blade”, “Ice Blade”, “Lightening Blade”, “Bash”, Inpower”, “Hostile Charge” // place these abilities in a string array?? } if (mobState == caster && mobLocation == "Ogre Den") { “Frostbolt”, “LighteningBolt”, “Firebolt”, “FlameWalker”, “FlameWalker Nova”, “Cleanse”, “Reflect”, “WaterWalker”, “WaterWalker Nova” // Place these spells in a string array?? } If (mobState == melee && mobLocation == “Ogre Den”){ “Bash”, “Overpower” // place these abilities in a string array?? } Return mobAbilities[]; }
Last edited by bulletbutter; 04-17-2008 at 09:43 AM.
Well, I would probably implement the multiple behaviors via mixins or the decorator pattern, but if you want to keep this design intact, make it a std::vector of std::string, and let the STL manage the allocation and copying. I'd be hard pressed to find a reason to use a variable length array instead of a vector or deque.
Another thing you could do is use a simple bitmask to store behaviors (e.g. behavior |= ICE_BLADE).
Last edited by medievalelks; 04-17-2008 at 09:54 AM.
Returning arrays requires creating it on the heap and returning it which is messy. Therefore the usual way is taking an argument as mats says. But in C++, there's a better way. Just return a std::vector. If the code isn't too complex, the compiler will optimize away the make a copy of the vector process (unnamed variable optimization, NRVO or something I think it's called?).
You can return mobAbilities as long as its a member variable. You'd actually be returning a pointer to the first element.
It looks like this:However, most likely, since you don't know what size the mobAbilities array needs to be for each Mob, you should use a vector instead of an array to store the information in the class. In that case returning that vector from a function should generally be done like so:Code:string *Mobs::getAbilities() { return mobAbilities; }
Code:vector<string> &Mobs::getAbilities() { return mobAbilities; }
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
That's easy enough to get around.
Code:#include <iostream> class unsafe { int i; public: unsafe(int) : i(j) {} const int & peek() { return i; } }; int main() { unsafe u(5); std::cout << u.peek() << std::endl; int & i = (int &) u.peek(); i++; std::cout << u.peek() << std::endl; return 0; }
There are always workarounds. You can't protect against everything.
If you return by const, then you've done right and the one who casts away the const does wrong.
It's possible to do this another way, too:
So whether or not you return a reference, the data isn't "safe," regardless of what you do. You can only ensure it's right to such a degree.Code:#include <iostream> class unsafe { int i; public: unsafe(int) : i(j) {} const int & peek() { return i; } }; int main() { unsafe u(5); std::cout << u.peek() << std::endl; int* i = (int*)&u; (*i)++; std::cout << u.peek() << std::endl; return 0; }
Yeah, const reference is best in this case. Should have put that in there.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.