Description
This example shows how to use Unnamed objects to work with static variables.
Unnamed objects of a given class are created when the constructor of that class
is invoked. For example the following statement UnnamedObjects.cpp UnnamedObjects.h Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
X().SetFloatMember( 3.141516 );
will create an unnamed object (via the compiler) and the value of the
static data member FloatMember will be set to pi.
Because unnamed objects are temporary, we use static data members so that
the value of the data will be shared amongst all temporary unnamed objects.
Notice that we haven't created any objects of either class explicitly.
Therefore the conventional object creation lines, such as
X oX;
Y oY;
are not used in this program.
Form 1 contains two TButton buttons, Button1 and Button2.
#include
#ifndef UnnamedObjectsH
#define UnnamedObjectsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class X
{
private:
static float FloatMember;
public:
X();
static void SetFloatMember( float f );
static float GetFloatMember();
};
class Y
{
private:
static float FloatMember;
public:
Y();
static void SetFloatMember( float f );
static float GetFloatMember();
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif