I want to update the stats of each actor in my program. But I will need to do it with a single generic function for all stats. At least I prefer to do it with one generic function. Failing that, I want to do it with a function for each stat. I wanted to do something like this:
Whereas anyactor will be the part I am having trouble figuring out. Is there a way to make it so that I can plug any actor (example joe, sally, jimmy) into the function? Maybe replacing anyactor with a reference or pointer of some type? I am looing to do it something like this:
1 2
//character is damaged for 5 points
changehitpoints(joe, 5, false);
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <string>
usingnamespace std;
class actor
{
public:
actor();
~actor();
string getname();
int getlevel();
int getstrength();
int getdexterity();
int getintelligence();
int getcharisma();
int getconstitution();
int getMAX_hitpoints();
int getCURRENT_hitpoints();
int getarmor();
void setname(string x);
void setlevel(int x);
void setstrength(int x);
void setdexterity(int x);
void setintelligence(int x);
void setcharisma(int x);
void setconstitution(int x);
void setMAX_hitpoints(int x);
void setCURRENT_hitpoints(int x);
void setarmor(int x);
protected:
private:
string name;
int level;
int strength;
int dexterity;
int intelligence;
int charisma;
int constitution;
int MAX_hitpoints;
int CURRENT_hitpoints;
int armor;
};
#endif // ACTOR_H
actor.cpp just has simple getter and setter functions so far.
For organisational purposes I want to keep that function separate. Besides, this is for education. Although it may seem like I am trying to make a game, i am only using this to learn new things in c++. The question, is essentially, how do I pass an object as an argument into a function separate from the objects class?
I will have no trouble including it as a class member, however, I wish to be able to take member names and pass them into classes to create non static members. I think I said that right.
I know I would have to do something like the following to create a character based on level. But I am journeying into new C++ frontiers.
Edit:
Basically, I want my function to be able to create an actor automatically by using something like this: createcharacter("Bob", 10); //Creates a character named Bob at level 10
ne555, I don't understand why you think it is obfuscated. Which part/s are you referring to? I have all variables in the class set up with setter and getter functions. The variables are private and the getters and setters are public. I thought was good programming practice. I am only starting to understand things like pointers and classes. I am still a beginner.
It is not clear what the third parameter represents and you don't actually need it. changehitpoints(joe, -42, false);
Be more critic with your code. ¿why are you doing that?
~50 LOC that basically do nothing can't be good practice.
Funny thing, your no-member functions interface is quite interesting.
By the way, if you are going to define functions in headers, make them inline or you could have redefinition issues.
And think about the consequences of defining functions in headers ;)
Thank you, ne555. This function was still in the experimental(Alpha) phase. It has since been bumped to the Beta phase of development. I wasn't sure what I was going to exactly do with it. Now I think I will be moving it to the actor class since it seems to be a perfect fit. Doesn't the GCC compiler make definitions of member functions inline already?
Doesn't the GCC compiler make definitions of member functions inline already?
Member functions defined inside a class declaration are implicitly inline. However, create_actor is not a member function. If you define a member function outside of the class declaration, it's not inline either.
Tanks for all of your help, everyone. ne555, I was just trying a different way to adjust hitpoints. But i like the joe.changehitpoints(-5); idea. I think i will stick with that. And add an action class to put any stat changing functions into. Thanks again.