C++ Builder

La Isla Bonitas



Pointers To Functions
Pointers To Functions


Description

Pointers to functions are usefull in situations in which we want to select different functions at run-time depending on the value of the input. The value of the input is not know until the program is run.

We use a typedef to define an alias T_doublepf for a pointer to a function that takes two double arguments and returns a double. Thus T_doublepf is a synonym for that type.

PToFuncTutorial.cpp

#include <vcl.h>
#pragma hdrstop

#include "PToFuncTutorial.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TComboBox* ComboBox1;
TLabel* Label1;
TLabel* Label2;
TButton* Button1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Width  = 500;
    Height = 400;
    Position = poScreenCenter;
    Color    = (TColor)0xcdcafee;

    String ComboItems[] = { "Sum", "Difference", "Multiplication",
                            "Division", "Mean" };

    ComboBox1 = new TComboBox( this );
    ComboBox1->Parent  = this;
    ComboBox1->Top     =  50;
    ComboBox1->Height  =  24;
    ComboBox1->Width   = 300;
    ComboBox1->Left    = (Width - ComboBox1->Width)/2;
    ComboBox1->Visible = true;   //default. Added for completeness
    ComboBox1->Enabled = true;   //default. Added for completeness
    ComboBox1->Color   = clBlack;
    ComboBox1->Font->Color = clWhite;
    ComboBox1->Font->Size  = 10;
    ComboBox1->Text        = String("Select math operation");
    ComboBox1->Style       = csDropDown;

    for ( int index=0; index < ARRAYSIZE( ComboItems ); index++ )
    {
        ComboBox1->Items->Add( ComboItems[index] );
    }

    Label1 = new TLabel( this );
    Label1->Parent = this;
    Label1->Top    =  100;
    Label1->Height =  24;
    Label1->Width  = 300;
    Label1->Left   = (Width - Label1->Width)/2;
    Label1->Font->Style = Label1->Font->Style << fsBold;
    Label1->Font->Size  = 10;
    Label1->Font->Name  = "Arial";
    Label1->Font->Color = clBlack;
    Label1->Caption     = "Result";
    Label1->Visible = true;   //default. Added for completeness
    Label1->Enabled = true;   //default. Added for completeness


    Label2 = new TLabel( this );
    Label2->Parent = this;
    Label2->Top    =  Label1->Top;
    Label2->Height =  24;
    Label2->Width  = 300;
    Label2->Left   = Label1->Left + 100;
    Label2->Font->Style = Label1->Font->Style << fsBold;
    Label2->Font->Color = clBlack;
    Label2->Caption     = "";
    Label2->Visible = true;   //default. Added for completeness
    Label2->Enabled = true;   //default. Added for completeness

    Button1 = new TButton( this );
    Button1->Parent = this;
    Button1->Top    = 300;
    Button1->Height =  40;
    Button1->Width  = 100;
    Button1->Left   = (Width - Button1->Width)/2;
    Button1->Font->Style = Button1->Font->Style << fsBold;
    Button1->Font->Size  = 10;
    Button1->Font->Name  = "Arial";
    Button1->Font->Color = clBlack;
    Button1->Caption     = "Calculate";
    Button1->Visible = true;   //default. Added for completeness
    Button1->Enabled = true;   //default. Added for completeness
    Button1->OnClick = Button1Click;


}
//---------------------------------------------------------------------------

double Sum( double x, double y )
{
    return x+y;
}
double Difference( double x, double y )
{
    return x-y;
}
double Multiplication( double x, double y)
{
    return x*y;
}
double Division( double x, double y )
{
    return x/y;
}
double Mean( double x, double y )
{
    return (x+y)/2.0;
}

int SwitchVar( String S )
{
    String Values[] = { "Sum", "Difference", "Multiplication", "Division",
                        "Mean" };

    for ( int i=0; i< ARRAYSIZE( Values ); i++ )
       if ( S == Values[i] )
           return i;
    return -1;   //Error: Illegal input
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{
    enum FunctionType
    {
        FT_SUM = 0,
        FT_DIFFERENCE,
        FT_MULTIPLICATION,
        FT_DIVISION,
        FT_MEAN,
    };

//We use a typedef to define an alias T_doublepf for a pointer to a
//function that takes two int arguments and returns a double.
//Thus T_doublepf is a synonym for that type

    typedef double (*T_doublepf)( double, double );  //alias
    T_doublepf pMathFunc;   //definition of the pointer to a function pMathFunc

    switch ( SwitchVar( ComboBox1->Text ) )
    {
        case FT_SUM:
             pMathFunc = Sum;
             break;
        case FT_DIFFERENCE:
             pMathFunc = Difference;
             break;
        case FT_MULTIPLICATION:
             pMathFunc = Multiplication;
             break;
        case FT_DIVISION:
             pMathFunc = Division;
             break;
        case FT_MEAN:
             pMathFunc = Mean;
             break;

        default: ShowMessage("Illegal Input. \nPlease enter one of the following operations\n\
1) Sum\n2) Difference\n3) Multiplication\n4) Division\n5) Mean\n");
             return;
        }

        Label2->Caption = FloatToStr( pMathFunc( 100, 3.141516 ) );
}

PToFuncTutorial.h

#ifndef PToFuncTutorialH
#define PToFuncTutorialH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Button1Click(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.