Advertisement

C++ class' member functions' arguments problem

Started by September 09, 2000 11:50 PM
5 comments, last by Alphathree 24 years, 3 months ago
I have a class (called "npc") defining attributes of a creature (it''s the beginnings of a role-playing game) and assigning various things they can do. One thing they can do is take_damage() when they get hit. And they can also melee_atk() another creature causing that creature to call its take_damage() function. Now, when I call take_damage() within my main() function it wors perfectly, reducing the creature''s health by the desired amount. However, when I call take_damage() from within another one of the member functions it doesn''t have any effect on the "npc" object''s health. This might sound a bit confusing, so I''ve moved the code into a text file on my website. If you look at the main function, I put a few comments in there so you can see what has an effect and what is not having an effect. I need the melee_atk() function to reduce the health of whatever npc is passed to it. (eg npc_one.melee_atk(some_npc) should reduce the health of "some_npc" because "npc_one" attacked it.) Here''s the code, please help: http://members.home.com/tomlevesque/tom.txt - Tom
Did I mention this is a really great message board?

I''ve been looking for boards like this for a while. I''m happy I found this set of boards! Expect to see me around here a lot. I don''t normally program C++, I''m mostly a website guru (java-script, Java, visual basic, html, dhtml, xml, yadda yadda yadda) so you''ll forgive me if my C-based skills are slightly limited.

I still need help with that program above though...

- Alphathree
Advertisement
void npc::melee_atk(npc targ)
{
targ.take_damage(1340);
}

This function''s arguments make a copy of the object passed to it. In your program, it passes a copy of Lady_Fox, and alters the health of the copy. So, you need to pass the actual Lady_Fox object by using the reference to it.

Like so:

void npc::melee_atk(npc& targ) // it just needs the ''&''
{
targ.take_damage(1340);
}

This will decrease the health of the right object.
I´d use pointer it does the same thing but you can check is it null*(In situation target has dead from poison)*.
    npc * target;target = new npc;SetTarget(Zealot);//Points to enemy Zealot isn´t this bad commentmelee_Attack();//user mouse input//User changes targetSetTarget(Me);    

You can´t change the place where referece points
The problem is as TV states, in C/C++ a function parameter is passed "by value" which means basically that a copy is made and the original is unafected. To get around this C programmers must pass pointers, so the pointer is still just a copy, but it has the ability to change the object pointed to (which is uncopied). In C++, the more common way of doing this, and much safer in many case (do to avoiding the unneeded extra power of pointers - IN THIS CASE) is to use a "reference" parameter. Follow TV''s advice and all will be well ... begin trying to set pointers to NULL and such and your in for a lot of headache until you get it ALL working perfectly. Cause if ONE place can change a pointer ... then EVERY place must check the value repeatadly, and if ONCE the pointer can possibly be NULL, then EVERYONE must ALWAYS check for that posibility ... as you can imagine, this situation makes general purpose pointer parameters LESS EFFICIENT than reference parameters, not because of the compiler .. which is identical .. but because pointers require more special case handling than reference .. so my vote is this ... when you don''t need any of the extra power a pointer provides, use a reference.
Thanks, that worked. I''m still a bit confused why though.

What does adding the & to this:

(npc ⌖)

actually mean? Does it mean "the npc at the same location in memory of the object that was passed"?

I haven''t used pointers much, so any clarification would be appreciated.

- Alphathree
Advertisement
In C, if you wanted to edit a data type or structure in a function you had to work by using the address of the data. This caused problems in two ways. The first is that the function argument needed to specify a pointer. When the function was to modify the data, it had to dereference the pointer. The second problem is that when the function was called somewhere in the program, it had to pass the values address.

In C:
void DecreaseByOne(int* VALUE)
{
(*VALUE)--; // Note the dereference
}

DecreaseByOne(*NUMBER); // pass the address .

In C++, references came along. These replaced the confusion of pointers. By "passing a value by reference", a value outside of the function can be altered with the ease as if it were inside it.

In C++:
void DecreaseByOne(int& VALUE)
{
VALUE--; // operates on the value passed
}

DecreaseByOne(NUMBER); // Doesn't use dereference

The "int&" in the function also means that it must pass a variable, you cannot pass a constant such as:


DecreaseByOne(8); // this is a no-no


Keep in mind that pointers are still used in C++, particulary in dynamic allocation.

Edited by - TV on September 10, 2000 11:20:25 AM

This topic is closed to new replies.

Advertisement