C++ Builder

La Isla Bonita



Unnamed Objects and their Constructors
Unnamed Objects and their Constructors

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

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.

UnnamedObjects.cpp

#include 
#pragma hdrstop

#include "UnnamedObjects.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//SECOND DECLARATION OF THE STATIC DATA MEMBERS FOR EACH CLASS
//AND INITIALIZATION:
float X::FloatMember = 0;
float Y::FloatMember = 0;

void X::SetFloatMember( float f )
{
    FloatMember = f;
}

float X::GetFloatMember()
{
    return FloatMember;
}

void Y::SetFloatMember( float f )
{
    FloatMember = f;
}

float Y::GetFloatMember()
{
    return FloatMember;
}


X::X()
{
   //TEST LINE
   //ShowMessage("X CONSTRUCTOR");
}

Y::Y()
{
   //TEST LINE
   //ShowMessage("Y CONSTRUCTOR");
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
   //INITIALIZE THE STATIC DATA MEMBER OF THE X CLASS
   //USING THE X CONSTRUCTOR
   X().SetFloatMember( 3.141516 );

   //INITIALIZE THE STATIC DATA MEMBER OF THE Y CLASS
   //USING THE Y CONSTRUCTOR
   Y().SetFloatMember( 2.718282 );
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//NOTE: SHOULD YOU REQUIRE FORMATTING, USE FloatToStrF()
//      INSTEAD OF FloatToStr()

   ShowMessage( "X Class: FloatMember =" + FloatToStr( X().GetFloatMember() )
                + "\n" +
                "Y Class: FloatMember =" + FloatToStr( Y().GetFloatMember() ) );
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button2Click(TObject *Sender)
{
//
    X().SetFloatMember( Y().GetFloatMember() );

    ShowMessage( "X().SetFloatMember( Y().GetFloatMember() ) = "
                  + String( X().GetFloatMember() ) );

    X().SetFloatMember( 10000.0 );
    Y().SetFloatMember( 1.0 );

    ShowMessage( "X Class: FloatMember() = "
                  + String( X().GetFloatMember() ) + "\n"
                  + "Y Class: FloatMember() = "
                  + String( Y().GetFloatMember() )
                  );

}

UnnamedObjects.h

#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

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.