2009-09-01 2 views
0

Hehe 질문 제목 선택에 어려움을 겪고 있습니다. 그러나 문제를 명확하게 설명하기 위해 문제에 대해 설명하겠습니다.클래스 함수에 의해 제어 될 클래스 인스턴스 등록

저는 이제 DirectX 래퍼가있는 C++ 게임을위한 자체 GUI 라이브러리를 작성하고 있습니다. 그러나 "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 벡터를 통해 제어 할 수 있습니까?

+0

GUIMAN-> RegisterWindow (FirstWindow); // ?? GUIMAN -> RegisterWindow (SecondWindow); 는 참조가 예상 될 때 포인터가 지나가는 것처럼 보입니다.이 문제가 실행중인 것입니까? –

+0

예! 실제로 WindowCollector의 각 멤버가 TargetWindow를 참조하여 RegisterWindow 함수로 다시 밀어 넣기를 원했습니다. –

답변

0

예 기본적으로 작동합니다. 왜 시도하지 않으시겠습니까? 하지만 루프 대신 반복자를 사용하고, 단지 new 대신 자동 포인터를 사용하는 것을 선호합니다 (현재 예제는 아마도 메모리 누수가 발생할 것입니다).

실제로 Google 검색 시작 std::vector ... 인수를 참조로 전달합니다.

0

질문을 올바르게 이해하지 못합니다. MyGUIWindow의 생성자에서 RegisterWindow()를 호출하고 소멸자에서 상응하는 등록 해제를 호출 할 수 있습니다. 그게 당신의 필요를 채우고 있습니까? 그렇지 않은 경우 명확히하십시오.