首页 > 解决方案 > 在哪里声明应用程序中使用的变量(wxWidgets)?

问题描述

我创建了MyApp继承自wxApp并添加了一些组件变量的类:

class MyApp : public wxApp {
    GameBoard* gb;
    Player* player;
    bool lightTheme;

    std::vector<wxBitmapToggleButton *> cardImages;
public:
    MyApp();
    virtual bool OnInit();
    void displayImages(wxFrame *, wxGridSizer *);

    void OnCardSelect(wxCommandEvent&);
};

然后我将它们的值设置为MyApp::OnInit

void MyApp::OnInit() {
    //...
    player = new Player(1);
    //...
}

当我Player在函数中使用类方法时MyApp::OnInit,一切正常,但是当我在事件函数中使用它时,我得到了奇怪的结果(例如,在类中声明的 大小返回 18446744073709551540)。事件功能:std::vector<int>Player

void MyApp::OnCardSelect(wxCommandEvent& event) {
    std::cout << (player->getChosen()).size() << std::endl;
    // above line only for debugging, getChosen() returns const std::vector<int>&, which should be empty, but it print strange result
    wxBitmapToggleButton *selectedCard = dynamic_cast<wxBitmapToggleButton *>(event.GetEventObject());
    player -> toggleChosen(int(selectedCard -> GetId()));
}

MyApp调用后OnInit(使用)的组件变量发生了什么IMPLEMENT_APP(MyApp),我应该在哪里声明和更改这些变量的值?

标签: c++user-interfacewxwidgets

解决方案


推荐阅读