在创建之前获取对象,是否可能

时间:2011-06-11 12:12:54

标签: c++

在创建对象之前是否可以获取对象?

像这样

template <class T>
class Event {
protected:
    T*Obj ; 
public:
    void onclick() { Obj->getobject()->dosomthing ; }  // i need help how to get the object
};

class myclass : public Event<myclass> {
public:
    Event<myclass>* getobject() {return Obj;}
    myclass* createobject() ; 
};

我尝试了一些代码和想法,但它总是失败,我什么都没得到(空指针)

我尝试指向成员的指针,指向函数的指针,但它也失败了! 如果我完全解释我想要的东西,我现在不会。

任何帮助?

这是完整的代码

template <class T>
class GUI_Event
{
private:
    static bool _Donothing (const EventArgs &Args) {return false ; }
    T*Obj ;

protected:

    /*for window only*/

    bool ( *CloseClicked )                     (const EventArgs &Args) ;
    /* event fired on left mouse click*/
    bool ( *_L_Mouse_Click_DoFunction )        (const EventArgs &Args) ;
    /* event fired on right mouse click*/
    bool ( *_R_Mouse_Click_DoFunction )        (const EventArgs &Args) ; 
    /*event  fired on middle mouse click*/
    bool ( *_M_Mouse_Click_DoFunction )        (const EventArgs &Args) ; 



public:



    /*set up fired function on left mouse click event*/

    const void Set_L_Mouse_Click_DoFunction( bool (*Function)(const EventArgs &Args))  {this->_L_Mouse_Click_DoFunction = NULL ; 
                                                                                           this->_L_Mouse_Click_DoFunction = Function ;
                                            Obj->Get_FrameWindowPtr ()->subscribeEvent ( FrameWindow::EventMouseClick , CEGUI::Event::Subscriber ( &_L_Mouse_Click_DoFunction ));} 

    /*set up fired function on right mouse event click */

};

class GUI_Window : public GUI_Event<GUI_Window>
{


private:


    static void SetID() ;
    /*
    Identifie Number For The Window
    */
    static int ID ; 

    /*
    /the Identifir Number for current window
    */
        int Wnd_ID ; 
        //Frame WIndow 
        CEGUI::FrameWindow        *_Frame_Window ;





        /* Window Name == value by the user */
        CEGUI::String             *_Window_Name ;


        /*window type == for now is inluded ( XML FIle (without extension)/FrameWindow )*/
        CEGUI::String             *_Window_Type;



        /* XML File that include style of the GUI   */
        String                    *_File_Name; 






public:

/*  create a Window */
        explicit GUI_Window ( CL_HUD const*Hud , String const&Window_Name );
        /*  create a Window */
        explicit GUI_Window ( const CL_HUD &Hud , String const&Window_Name );


    /*
    return current framewindow As Pointer 
    */
    FrameWindow              *const Get_FrameWindowPtr ()const                      { return _Frame_Window ; }
        /*
    return current framewindow 
    */
    const FrameWindow        &Get_FrameWindow ()const                               { return *_Frame_Window ; }





    /*return current window type*/
    const String            &Get_WindowType()  const                               { return *_Window_Type ;}
    /*return current window type As Pointer */
    String                  *const Get_WindowTypePtr()  const                      { return _Window_Type ;}



    /*return current window name As Pointer  */
    String                   *const Get_WindowNamePtr()  const                     { return _Window_Name ;}
    /*return current window name*/
    const String             &Get_WindowName()  const                              { return *_Window_Name ;}


    /* return XML File Name Scheme */
    const String             &Get_File_Name()  const                               { return *_File_Name ;}
    /* return XML File Name Scheme As Pointer */
    String                   *const Get_File_NamePtr()  const                      { return _File_Name ;}


    void SetText(String text )                        { Get_FrameWindowPtr ()->setText ( text ); *_Window_Name = text ; }
    int Get_ID ()                                                                  { return Wnd_ID ; }
    ~GUI_Window();


};

问题是我在下一行

中得到一个空指针
Set_L_Mouse_Click_DoFunction

4 个答案:

答案 0 :(得分:1)

简短回答:是的。但是你的对象不会被创建,所以当你'得到它'时它将为null,如果你使用它,可能会发生坏事。

简短的回答:什么?

class Event
{
public:
bool onclick()
    { getobject->dosomthing ; }  //// getobject is a method.
                                 //// It should have parenthases
};

class myclass : public  Event
{
private:
Obj ;                  //// This is odd.  What is Obj?
                       //// Is it supposed to be a type or a variable name?
                       //// a line like the following would create the object on the stack:
                       //// myclass Obj;
 public:

  getobject() {return Obj;}
  createobject() ; 
};

答案 1 :(得分:0)

您可以使用继承

class Event
{
public:
    virtual bool onclick() { /* default behavior here */ }
    virtual ~Event() { } // virtual destructor for a base class.
}

class MyClass : public Event
{
public:
    MyClass() { myObj = new Obj(); }

    bool onclick() { myObj->DoSomething(); }

    ~MyClass() { delete myObj; }
private:
    Obj* myObj;
}

答案 2 :(得分:0)

您的意思是Lazy Loading您的对象吗?

答案 3 :(得分:0)

我回答这个问题

set_Object (T*newObj)     {Obj = newObj ; }
我在GUI_Window中

set_Object(this)