我想创建一个从 TPanel 派生的新组件。这个新组件只有一个私有字段:“obj”(一个 TObject)。
在构造函数中,我创建了对象。后来,当我尝试访问该对象时,它为 NULL。为什么?
标题:
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); }