[英]C++ - static objects/variables used across a project vs. using pointer/reference arguments
I'm writing a simple memory game (the one with the cards you flip) with C++ and SDL, and I'm finding it difficult to decide whether I'd use a class such as this: 我正在用C ++和SDL编写一个简单的内存游戏(带有翻转卡片的游戏),但发现很难决定是否要使用这样的类:
class Game {
public:
static StartMenu* sMenu;
static OptionsMenu* oMenu;
static GameBoard* board;
static Card cards[36];
}
And then refer to them like this 然后这样称呼他们
Game::menu->selectedItem = 1;
Or should I just construct the project so that I'd instantiate the objects somewhere and then pass them to functions as pointers/references (I'll figure out which ones I should use later)? 还是我应该只构建项目,以便在某个地方实例化对象,然后将它们作为指针/引用传递给函数(我将在以后弄清楚应该使用哪些对象)? Like this 像这样
void processInput(SDL_Event event, StartMenu*&*& menu) {
...
menu->selectedItem = 1;
...
}
Or is it just a matter of preference? 还是只是偏爱问题? The latter one seems "cleaner" but the former is more flexible... 后者似乎“更干净”,但前者更灵活...
I would recommend using: 我建议使用:
class Game {
public:
StartMenu* sMenu;
OptionsMenu* oMenu;
GameBoard* board;
Card cards[36];
};
Create an instance of Game
and pass it around. 创建一个Game
实例并将其传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.