Answer the question
In order to leave comments, you need to log in
How can C++ create an instance of a class that is visible from the methods of another class?
Actually, how to make an object available anywhere?
The first thing that came to mind:
CGameHud* CGameHudInstance;
int main(int argc, char** args)
{
CGameHudInstance = new CGameHud(argc, args);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Strictly speaking, the use of global variables in C++ is rare. This is legal, but mostly for compatibility with C. C++ uses other techniques to pass context, such as "passing by pointer" (below). It's also worth mentioning that global variables come with some nuances, such as problems with the initialization sequence.
But judging by your question, you don't need global variables. It will be enough code like this:
/*
* gameinstancehandler.h
*/
class GameInstanceHandler{
public:
GameInstanceHandler(CGameHud *instance):
mInstance(instance){}
private:
CGameHud *mInstance;
// something other here
}
#inlucde <gameinstancehandler>
int main(int argc, char **args){
CGameHudInstance *gameHub = new CGameHud(argc, args);
GameInstanceHandler *handler = new GameInstanceHandler(gameHub);
return 0;
}
int *a; // хорошо, a — это указатель.
int* b; // плохо, но допустимо.
int* c, d; // совсем плохо, c — указатель на int, d — просто переменная типа int.
int *e, *f; // при такой же записи все понятно сразу.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question