S
SearedIce
Consider the following simplified hypothetical code:
#include <iostream.h>
class rabbit
{
public:
rabbit() {x = 3; y = 3; /*code here to set field[x][y] to 1*/}
void runtocage();
int x;
int y;
}
void rabbit::runtocage()
{
//code here to set field[x][y] to 0 and field[0][0] to 1
}
class garden
{
public:
void monsterattack();
rabbit fluffy;
bool field[5][5];
}
void garden::monsterattack()
{
fluffy.runtocage();
cout << "Ahh!!";
}
//End of code segment
This not at all what I'm really working on...I'm working on a linked
list for something, but what I show here is what is almost necessary
for what I'm doing...just thought the rabbit/garden thing would be cute
and clearly show what I'm wondering about...
Now for the question:
In the two places I have commented in the rabbit code, how can I do
what I wrote?
Could I add a member variable to rabbit that is a pointer to a garden
(garden * homesweethome; for instance) and give it the address of the
garden in the rabbit constructor when the rabbit is created in the
garden? (in garden code: "rabbit fluffy(this);" instead of "rabbit
fluffy;" and then change the rabbit constructor to: "rabbit(garden
home) {x = 3; y = 3; homesweethome = home; homesweethome->field[x][y] =
1;}" then do some similar code as that last command in the runtocage()
function to change the "field"...)
Won't that cause a compile error, however, because whichever class I
put before the other, the compiler will tell me it doesn't know what
one of them is when it is referred to in the other.
I'm probably missing something simple...there are some basic things
I've missed in the book's I've read and the classes I've taken...
Thanks for your time,
John
#include <iostream.h>
class rabbit
{
public:
rabbit() {x = 3; y = 3; /*code here to set field[x][y] to 1*/}
void runtocage();
int x;
int y;
}
void rabbit::runtocage()
{
//code here to set field[x][y] to 0 and field[0][0] to 1
}
class garden
{
public:
void monsterattack();
rabbit fluffy;
bool field[5][5];
}
void garden::monsterattack()
{
fluffy.runtocage();
cout << "Ahh!!";
}
//End of code segment
This not at all what I'm really working on...I'm working on a linked
list for something, but what I show here is what is almost necessary
for what I'm doing...just thought the rabbit/garden thing would be cute
and clearly show what I'm wondering about...
Now for the question:
In the two places I have commented in the rabbit code, how can I do
what I wrote?
Could I add a member variable to rabbit that is a pointer to a garden
(garden * homesweethome; for instance) and give it the address of the
garden in the rabbit constructor when the rabbit is created in the
garden? (in garden code: "rabbit fluffy(this);" instead of "rabbit
fluffy;" and then change the rabbit constructor to: "rabbit(garden
home) {x = 3; y = 3; homesweethome = home; homesweethome->field[x][y] =
1;}" then do some similar code as that last command in the runtocage()
function to change the "field"...)
Won't that cause a compile error, however, because whichever class I
put before the other, the compiler will tell me it doesn't know what
one of them is when it is referred to in the other.
I'm probably missing something simple...there are some basic things
I've missed in the book's I've read and the classes I've taken...
Thanks for your time,
John