forgot proper way to get info of one class into another class

ok im trying to setup my game. first I have my main.cpp which just creates
1
2
3
4

Game game;
game.run();


I also have my Player class but having trouble accessing the player information from the game class. how can I make it so that my game class has access to player class? or any other class for that matter as my game class will be running everything

1
2
3
4
5
6
7
8
9
10
11
12
13
Player player("Bob");

Game game;
game.run(player);


....


void Game::run(Player& player)
{
    player.getStuff();
}


if you have lots of stuff to pass into game, then maybe create a class that holds all the data and pass that in. OR you could create stuff in the game class itself rather than passing in.
Last edited on
ok that works for the game.run(player) function but not for the rest of the functions in the game class, Once im in the game.run() function I have game.update() function as well as others
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct player { /* .... */ void foo() { /* ... */ } int n = 25 ; };

struct game
{
    explicit game( player& p ) : the_player(p) {}

    void bar( int v ) { the_player.foo() ; the_player.n += v ; }
    // ...

    private: player& the_player ;
};

int main()
{
    player morngrym ;
    game solitaire(morngrym) ;
    solitaire.bar(7) ;
}
Topic archived. No new replies allowed.