C++ Builder

La Isla Bonitas



C++ Builder Special Features
C++ Builder Special Features


Abstract

With C++ Builder, Borland has introduced one of the finest compilers in the market ever. In this section I present some of the advanced features of Borland Builder. Theses features include:

1) Assigning pointers to functions to a variable of type void*.




Assigning pointers to functions to a variable of type void*

Consider the following code
int Function()
{
    return 1;
}

void* Func( int i )
{
    return Function;
}
             
Most compilers will issue a compile-time error, such as this one:
"return value type does not match the function type return Function"
Builder, however, will compile it without errors and will execute it correctly. This is another example:

FuncReturningPointerToFunc.cpp

#include <vcl.h>
#include <limits.h>
#pragma hdrstop

#include "FuncReturningPointerToFunc.h"

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


int Function1()
{
    ShowMessage( "Function1()" );
    return INT_MAX;
}

float Function2()
{
    ShowMessage( "Function2()" );
    return 1000.0;
}

double Function3()
{
    ShowMessage( "Function3()" );
    return 100000000000;
}

void* Func( int i )
{
    switch ( i )
    {
       case 0: return Function1;
       case 1: return Function2;
       case 2: return Function3;
       default: ShowMessage("Illegal input");
    }           return NULL;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
     int i    = (* (int (*)()) Func(0))();

     float f  = (* (float (*)()) Func(1))();

     double d = (* (double (*)()) Func(2))();

     ShowMessage("i = " + String (i)     + "\n" +
                 "f = " + FloatToStr (f) + "\n"
                 "d = " + FloatToStr (d) );
}

FuncReturningPointerToFunc.h

#ifndef FuncReturningPointerToFuncH
#define FuncReturningPointerToFuncH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

I will add more features in the near future.

Homepage

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