Sign in to follow this  
Bykolak

[C++] Using fields of one object in another object`s function

Recommended Posts

I`m quite sure it won`t work this way but maybe you guys know how to the same thing but in proper way.


Here`s an example what i want to do.

[code]
class cFoo ()

{

int x,y;

//...

};





class cFoo2()

int a;

void Some_Function()

{

a=object1.x;

};

main()

{

while(!finish)

{

cFoo object1;

cFoo2 object2;

object.Some_Function();

}

}

[/code]

Share this post


Link to post
Share on other sites
Syntax errors aside while assuming you aren't using an esoteric language...

If you want to access the data, you need to pass a reference or copy of the value to the function:
[code]
// ...

class cFoo2
{
int a;

void Some_Function(const cFoo &foo)
{
a = foo.x;
}
}

int main()
{
while(!finish)
{
cFoo object1;
cFoo2 object2;
object2.Some_Function(object1);
}
}
[/code]

If you give us your high level goal, we might be able to suggest a better overall design for this.

Share this post


Link to post
Share on other sites
[code]class cTile
{
public:
int x,y;
int level,terrain,income;
int own;
cTile()
{
x=0;y=0;level=0;terrain=0; own=0;income=0;
}
void Check_Income()
{
if(terrain==GRASS) income=5*level;
if(terrain==FORREST) income=10*level;
if(terrain==HILLS) income=7*level;
if(terrain==MOUNTAINS) income=5*level;
if(terrain==CASTLE) income=20*level;
}

};[/code]




[code]class cMap


{
public:
cTile pole[MAP_X][MAP_Y]; //<- i was talking about this field. It contains all data about each Tile
cScroll scroll;
int map_type;
void Show(); //shows map


void Check_Scrolling();

void Show_Mini();

void Load(int _type, const char* nazwa);

}

[/code]

Ok thats cTile and cMap classes. Now i started to write a new class called cCountry.




[code]class cCountry
{
public:
short int ID,AI;
int gold,food,wood,iron,magic,army;
int gold_income,food_income,wood_income,iron_income,magic_income,army_upkeep;
cCountry()
{
gold=0;food=0;wood=0;iron=0;magic=0;army=0,AI=COMPUTER;
}

void Update_Incomes()
{


food_income=0;

int i,t;
for (i=0; i<MAP_Y; i++)
for (t=0; t<MAP_X; t++); // here i wanted to do something like if(map[t][i].terrain==grass) food_income+=map[t][i].income;

}
};[/code]

and at the end some of my main function

[code]int main()
{
int i=0;
cCountry coutries[MAX_COUNTRIES];
for (i=0; i<MAX_COUNTRIES; i++) countries[i].ID=i;
countries[3].AI=PLAYER;
cMap map;

while (!key[KEY_ESC])
{

//here is main loop and i wanted to update my incomes somewhere here.

}

};

[/code]

I would love some design tips instead of getting code. Its better for learning :)

Share this post


Link to post
Share on other sites
Ok, you have a set of tiles, and you want to accumulate their various income levels. I would do something like this:
[code]
struct Income
{
int gold, food, wood, iron, magic;

Income() : gold(0), food(0), wood(0), iron(0), magic(0)
{
}
};

class Tile
{
public:
// ...

void collectIncome(Income &income) const
{
switch(terrain)
{
case GRASS: income.food += income; break;
case MOUNTAIN: income.gold += income; break;
// ...
}
}

bool ownedBy(int playerId) const
{
return owner == playerId;
}
};

class Map
{
public:
// ...

Income calculateIncome(int playerId) const
{
Income income;

for (int y = 0; y < MAP_Y; ++y)
{
for (int x = 0; x < MAP_X; ++x)
{
// N.B. I'm indexing [y][x] because that is the way the processor loves to fetch data
// For this to work you need to change the order of the constants when dimensioning the tiles array.
const Tile &tile = tiles[y][x];

if(tile.ownedBy(playerId))
{
tile.collectIncome(income);
}
}
}

return income;
}
};

// Elsewhere

void Country::update(const Income &income)
{
// Apply income values to members...
}

int main()
{
// ...

while(running)
{
// ...

for(int i = 0 ; i < MAX_COUNTRIES ; ++i)
{
Country &country = countries[i];
Income income = map.calculateIncome(country.ID);
country.update(income);
}
}
}
[/code]
In this way, only the tile cares about what tile type it is. I would recommend using an enumeration for the tile types BTW.

I'm not sure you actually need to store these incomes as members, they are probably only used once during the loop, and seem to be reset every step. In particular, you should not leave any members uninitialised in a constructor. This is a recipe for weird bugs. I refactored them into a transient value based on the assumption that you don't need to store them.

Arguable the Map class should not have to care about Income. For the moment this is a simple solution. If the map class gets much bigger, I would probably split this off into a free function, and expose some member functions in the Map class to allow clients to safely iterate over the tiles.

I would advise you don't bother using [i]short[/i], unless you have a really compelling reason (e.g. millions of records). Requirements change, and you'll have bugs someday because you originally though you would only need 2[sup]8[/sup] or 2[sup]16[/sup] possible values, but later you end up using more without changing that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this