代码之家  ›  专栏  ›  技术社区  ›  IceCold

如何创建自定义组件?访问专用字段时出错

  •  0
  • IceCold  · 技术社区  · 7 年前


    标题:

    class PACKAGE TMyClass : public TPanel
    {
    private:
        TObject *obj;
    protected:
    public:
       __fastcall TMyClass(TComponent* Owner);
       void Stuff();
    };
    

    CPP文件:

    __fastcall TMyClass::TMyClass(TComponent* Owner)
       : TPanel(Owner)
    {
        Caption        = "";
        DoubleBuffered = True;
        Width          = 385;
        Height         = 65;
    
        TObject *obj= new TObject;     //obj gets an address here
    }
    
    
    
    void TMyClass::Stuff()      // <---- I call this method in the OnClick event of a button.
    {
       Caption = obj->ClassName();    //obj is NULL here
    }
    //---------------------------------------------------------------------------
    
    
    
    
    
    
    
    
    namespace Uvolctrl
    { void __fastcall PACKAGE Register()
       {  TComponentClass classes[1] = {__classid(TMyClass)};
           RegisterComponents(L"Samples", classes, 0); } }
    
    
    
    static inline void ValidCtrCheck(TMyClass *)   // assure that the components do not have any pure virtual functions.
    { new TMyClass(NULL);    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Remy Lebeau    7 年前

    在构造函数中创建一个类实例并将其赋给一个名为 obj 而不是你的私人 目标