可以在创建对象之前获取对象

问题描述

| 是否可以在创建对象本身之前获取对象? 像这样
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
    

解决方法

        长短答案:是的。但是您的对象不会被创建,因此当您获取对象时,该对象将为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() ; 
};
    ,        您可以使用继承:
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;
}
    ,        您是说懒加载对象吗?     ,        我这样回答我的问题
set_Object (T*newObj)     {Obj = newObj ; }
在GUI_Window我
set_Object(this)
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...