Archived

This topic is now archived and is closed to further replies.

graveyard filla

question about classes

Recommended Posts

is it possible for classes to have internal functions that can access parameters from outside of the class? what im trying to say is, even if a function is public, is it possible to pass it parameters from outside of the class. im just learning about classes now and i cant seem to get the internal functions to recieve any parameters. its saying that its an undeclared identifier. (if i make the variables global it all works fine. but is this the only way?) so can you only pass parameters to class functions from within the class itself? ps i do have everything as public. [edited by - graveyard filla on January 15, 2004 3:26:18 AM]

Share this post


Link to post
Share on other sites
is there some special syntax for sending a parameter to a class's fucntion? i have a class called enemy. one of the class's function is attack(). attack will calculate the enemies attack and return the players health. i do

class enemy
{
public:

void attack();
};

//then right after that, i would declare and write the function


void enemy::attack(int &player_health)


{
calculate damage done, set new value for player_health;
player_health = player_health - damage;
}


enemy monster;

now, somewhere else down in the program is a function that handles all battles. and i get to the part where i want my monster or whatever to attack and put

monster.attack(player_health);

well, everything runs smooth, except player health isnt updated. its not sending back the value and updating. no matter how much damage gets done to me, my health stays the same. what i did was change player_health to a global variable and deleted whereever it was passed around as a parameter. the program ran fine with no problems, health was updated as usual.

this is just a small text based rpg that ive been working on. i just learned about classes so i thought id try implementing classes into the program (so far i see that to fully implement classes properly id have to completely re write the whole program). i just wanted to play around with classes to see if i could do it myself. i ran into this problem.


ps this isnt actualy source code i just tried to sum it up for you all



[edited by - graveyard filla on January 15, 2004 4:19:48 AM]

Share this post


Link to post
Share on other sites
What you could do is to make a Player class, and give the enemy::attack function a reference to the player object it is fighting. In the enemy::attack function you could then call a player::damage(int damage) function. In this new function you then update the player''s state.

Did that make sense? Or do you want some source/pseudo-code?



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)

Share this post


Link to post
Share on other sites
There shouldn''t be anything wrong with what you''re doing except the void attack() in your class definition doesn''t match up with void enemy::attack(int &player_health). You probably forgot something in your actual code. Passing player_health by reference is enough to keep any changes made to it after the function ends.





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Original post by rohde
What you could do is to make a Player class, and give the enemy::attack function a reference to the player object it is fighting. In the enemy::attack function you could then call a player::damage(int damage) function. In this new function you then update the player''s state.

Did that make sense? Or do you want some source/pseudo-code?



Human beings, by changing the inner attitudes of their minds, can change the outer aspects of their lives.

William James (1842 - 1910)


psuedo/source would be great. thanks ! . and DO i have to send a classes functions parameters from another class, or can it be from anywhere in the program?

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
well, everything runs smooth, except player health isnt updated. its not sending back the value and updating.

Presumably because somewhere a copy of player_health is implicitly made and passed to attack. However, since you''re not showing us real code, its not possible to say exactly what you are doing wrong. Hint: don''t pass player_health, pass the entire player object and have attack call member functions on the player object so that it may decide for itself how much damage has been inflicted. That way, you will be able to localise code to the parts of the program where it is relevant.
quote:

ps this isnt actualy source code

Then we can''t actually tell you what the problem is.

Share this post


Link to post
Share on other sites
how do you pass an entire object? when you pass an object, does it also pass all the objects functions and variables/data with it? or do you have to do it all one by one? do you have to pass the class too, or does it know allready what the class can do?

sorry if these seem like stupid questions. i have only so far learned a little about classes/OOP and it seems hard to grasp at first. but i have a basic understanding of it..... i think?

[edited by - graveyard filla on January 15, 2004 7:32:58 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
how do you pass an entire object? when you pass an object, does it also pass all the objects functions and variables/data with it? or do you have to do it all one by one? do you have to pass the class too, or does it know allready what the class can do?


Ok, time for a little tutorial on how this stuff looks in memory.

Let''s say I have

class Foo {
int bar;
static double baz;
void wibble() {}
}

When this is compiled, the following things end up in memory (specifics may vary by implementation and language, but this is an accurate enough model, assuming my brain isn''t completely fried at the moment):

1) The class data including vtable.
This is a structure that contains:
- the static double baz.
- the "vtable", which is an array of function pointers. In this case there will be one element, a pointer to the code for the wibble() function (which is empty here).
- Some other bookkeeping stuff.

2) For every instance of the object: an ''object'' structure containing
- the non-static int bar.
- a pointer to the class data.
- Possibly some other bookkeeping stuff, like a reference count, ''lock'' field (in Java, you can use any object for synchronization), etc.

To "pass the object", you either
1) Pass in a pointer to the object.
2) (C++ only) Pass the object by reference.

Everything is bundled together; either way the value that your function receives is a *memory address* where the object is located. Then you call object methods on it.

The compiler knows at this point "what the object can do" because of the method signature.

Putting that all together, you should be able to write something like:

class Enemy {
public:
void attack(Player &p);
}

void Enemy::attack(Player &p) {
int damage = do_calculations();
// your calculations might in turn depend on other information

// about the player, which you could get through other methods

// called on p.

p.takeDamage(damage);
}

class Player {
private:
int health;
public:
void takeDamage(int damage);
}

void Player::takeDamage(int damage) {
health -= damage;
if (health < 0) gameOver();
// See how nice this bit of ''encapsulation'' is - think about

// how many places in your code you would normally have to write

// this check. :)

}


[rant]When I was learning C++, I had already studied C and had a thorough grasp of pointers. The addition of references in C++ actually *confused* me because of how they combine pointer-like behaviour behind the scenes with actually-having-the-struct syntax. Now I work mostly in Java, which re-simplifies things by having everything be a pointer, except that you use .''s instead of ->''s. Needing both pointers and references in the same language is really awful IMHO.[/rant]

Share this post


Link to post
Share on other sites