Including class objects in functions

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:
1
2
3
4
5
6
7
void changehitpoints(anyactor, int amount, bool add_subtract)
{
    if(add_subtract == true)
    anyactor.sethitpoints(anyactor.gethitpoints() + amount);
    if(add_subtract == false)
    anyactor.sethitpoints(anyactor.gethitpoints() - amount);
}

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);

...Rather than needing to do it this way:
1
2
3
4
5
joe.sethitpoints(joe.gethitpoints() - 5);
//or
int damage;
damage = 5;
joe.sethitpoints(joe.gethitpoints() - damage);

Does this make sense?

Here is my actor header file, if it helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef ACTOR_H
#define ACTOR_H

#include <iostream>
#include <string>

using namespace 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.
There's no reason you couldn't do a joe.changehitpoints(5, false);
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?
Then passing by reference or pointer is the way to go. You should be able to find tutorials that will explain it way better than I can.
Thanks.

Edit:

I tried passing by pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    string nname;
    int llevel;
    actor jo;
    actor *pete;
    pete = &jo;
    cin >> nname >> llevel;
    jo.setname(nname);
    jo.setlevel(llevel);
    cout << "name " << jo.getname() << "  " << "level " << *pete.getlevel();

	return 0;
}

But all I got was this eeror:
F:\CodeBlocks-Programs\LittleRPG\main.cpp|19|error: request for member 'getlevel' in 'pete', which is of non-class type 'actor*'|
Last edited on
Use pete->getlevel()

Thank you, mik2718.

I will try to explain better what I am trying to do.
This is my function header file so far. It has any non class functions in it...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef LRPG_FUNCTIONS_H_INCLUDED
#define LRPG_FUNCTIONS_H_INCLUDED

#include <iostream>
#include <string>
#include "actor.h"

using namespace std;

create_actor(string name, int lvl);

void create_actor(string name, int lvl)
{
    actor.setname(name);
    if(lvl == 1)
    {
        actor.setlevel(1);
        actor.setarmor(5);
        //etc...
    }
}

#endif // LRPG_FUNCTIONS_H_INCLUDED 

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.
1
2
3
4
5
6
7
8
9
10
11
12
void actor::create(string _name, int _level)
{
    name = _name;
    level = level;
    if(level == 1)
    {
        armor = 5;
        strength = 10;
        //etc...
    }
    //etc...
}


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
Last edited on
It looks like the class actor has only the default constructor - actor()

You can add another constructor that takes some arguments and sets the actors internal values

1
2
3
4
5
6
actor::actor(const string& _name,int_level)
  :  name(_name), level(_level)
{
   // any other initialisations

}


this code uses an initialisation list.

With so many internal things to set in actor you might want to have some default parameters.

If you want a set of default values and you want to deviate from this for arbitrary arguments then this is not easy in C++
The named parameter idiom is a work around
http://www.parashift.com/c++-faq-lite/ctors.html
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Named_Parameter


How does this look? It will require a little tuning. But I like the level by experience equation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*****************Level Prgression*****************/
/*
exp/Lvl/difference from previous
0/1/0
50/2/50
150/3/100
300/4/150
500/5/200
750/6/250
1050/7/300
1400/8/350
etc...
*/
/**************************************************/

using namespace std;

void setup_character(actor &a, int n)
{
    a.setexp(n);
    a.setlevel((25 + sqrt(625 + 100 * a.getexp())) / 50);
    a.setcharisma(a.getlevel()*3);
    a.setconstitution(a.getlevel()*3);
    a.setdexterity(a.getlevel()*3);
    a.setintelligence(a.getlevel()*3);
    a.setMAX_hitpoints(a.getlevel() + (a.getconstitution() * 2));
    a.setstrength((a.getdexterity() + (a.getconstitution()*2))*10);
}
¿Why aren't your members public?
By the way void changehitpoints(anyactor, int amount, bool add_subtract) is obfuscated.
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.
Last edited on
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.
Topic archived. No new replies allowed.