ID:1663332
 
I know this is probably a basic question, but does anybody know how to call a member function of a class instance from inside a scope in which the instance is not defined?

For example, I have a Game class and a Player class. I want a member function inside of the Game class to access a member function of an instance (for example, Nate) of the Player class and get the return value. In other words, I want the instance Player::Nate to call a function in the gameLoop() function that is part of Game::game.
Pass a pointer or reference to Game::game to the instance Player::Nate and call it like this: (partially assumes C++11)

#include <iostream>
#include <memory>

class Player;
class Game;

int main(int argc,char** argv){
std::shared_ptr<Game> game = std::make_shared<Game>();
std::shared_ptr<Player> Nate = std::make_shared<Player>(game);

return 0;
}

class Player{
private:
// vars here
public:
Player(std::shared_ptr<Game> game){if(game!=nullptr){game->gameLoop();}
};

class Game{
private:
// vars here
public:
Game()
void gameLoop();
};
I'll give it a try when I get home today - thanks :)
In response to Pokemonred200
Kids these days and their damn shared_ptr's. I remember when all we had was a stupid star and we had to delete everything manually.
In response to ExPixel
ExPixel wrote:
Kids these days and their damn shared_ptr's. I remember when all we had was a stupid star and we had to delete everything manually.

So do I, actually. It's how I was first taught. But these shared_ptrs make life easier :D