I want to create a new component derived from TPanel. This new component has only one private field: "obj" (a TObject).
In the constructor I create the object. Later, when I try to access the object it is NULL. Why?
Header:
class PACKAGE TMyClass : public TPanel
{
private:
TObject *obj;
protected:
public:
__fastcall TMyClass(TComponent* Owner);
void Stuff();
};
CPP file:
__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); }
In your constructor you're creating a class instance and assigning it to a local variable called
obj
and not your privateobj
member variable.