question about classes

Started by
10 comments, last by graveyard filla 20 years, 3 months ago
you can pass a pointer or reference to the objects...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Advertisement
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]

This topic is closed to new replies.

Advertisement