C++ 为要由类的函数控制的对象注册类实例

C++ 为要由类的函数控制的对象注册类实例,c++,C++,呵呵,我很难选择题目。但让我解释一下我的问题,让它更清楚 我现在用C++编写了我自己的GUI库,里面有DirectX包装器。 但是我不知道如何通过调用manager类的draw函数来渲染游戏中的窗口 例如,假设我已经有了一个管理器和窗口类 经理: class MyGUIManager { private: std::vector<MyGUIWindow> WindowCollector; public: MyGUIManager() { } virtual MyG

呵呵,我很难选择题目。但让我解释一下我的问题,让它更清楚

我现在用C++编写了我自己的GUI库,里面有DirectX包装器。 但是我不知道如何通过调用manager类的draw函数来渲染游戏中的窗口

例如,假设我已经有了一个管理器和窗口类

经理:

class MyGUIManager { private: std::vector<MyGUIWindow> WindowCollector; public: MyGUIManager() { } virtual MyGUIWindow *NewWindow(char *szWindowTitle) { MyGUIWindow *temp = new MyGUIWindow(); temp->SetWindowTitle(szWindowTitle); return temp; } void RegisterWindow(MyGUIWindow targetWindow) // hopely this { this->WindowCollector.push_back(targetWindow); } void Draw() { // I wanted this function to be able to call all MyGUIWindow instances' Draw() function // can it be helped like this? for(int i = 0; i < this->WindowCollector.size(); i++) this->WindowCollector.at(i)->Draw(); // but the vector members must be referenced to each window instance.. } }; 窗口:

class MyGUIWindow { public: MyGUIWindow() { this->SetWindowTitle("New Window"); } void SetWindowTitle(char *szWindowTitle); void Draw(); }; 主要节目是:

//... MyGUIManager *GUIMAN = new MyGUIManager(); MyGUIWindow *FirstWindow = GUIMAN->NewWindow("Hello World"); MyGUIWindow *SecondWindow = GUIMAN->NewWindow("Hello World!!!"); GUIMAN->RegisterWindow(FirstWindow); // ?? GUIMAN->RegisterWindow(SecondWindow); while(Drawing()) { GUIMAN->Draw(); // I wanted this function to be able to call ALL MyGUIWindow instances' Draw() function //... }
因此,主要的问题是,如何使所有MyGUIWindow变量都可以通过WindowCollector向量进行控制?

我不确定我是否正确理解了您的问题。您可以在MyGUIWindow的构造函数中调用RegisterWindow,并在析构函数中调用unregister等效项。这能满足你的需要吗?如果没有,请澄清。

是的,基本上可以,为什么不试试呢?但是我更喜欢使用迭代器而不是循环,而且一些自动指针而不是当前的新示例可能会导致内存泄漏


实际上,从谷歌搜索std::vector开始。。。并将参数作为引用传递。

GUIMAN->RegisterWindowFirstWindow;//??GUIMAN->RegisterWindowSecondWindow;看起来你在期望引用时传递了指针,这是你遇到的问题吗?是的!实际上,我希望WindowCollector的每个成员在RegisterWindow函数中向后推时引用targetWindow。