Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of struct with function pointers

Status
Not open for further replies.

JohanErlank

Programmer
Apr 12, 2011
2
ZA
Hi

I am trying the following:

file.h:

typedef struct
{
int iBit;
void (*save_fun)(void);
} __save_rec;

...

#define MAXRECS 20

class TForm1 : public TForm
{
__published:
..
private:
__save_rec SaveData[MAXRECS];
void SavPan(void);
void SavAmt(void);
void SavFD7(void);

void __fastcall FillArray();
void __fastcall ExecFun(int iVal);
public:
...
};
...

file.cpp:

...

void __fastcall TForm1::FillArray()
{
SaveData[0].iBit=0;
SaveData[0].save_fun=SavPan;
...
}

void __fastcall TForm1::StepVals(void)
{
for (int i=0;i<10;i++)
{
ExecFun(i);
}
}

void __fastcall TForm1::ExecFun(int iVal)
{
if(iVal==SaveData[iVal].iBit)
SaveData[iVal].save_fun();
}

but it does not work. I get all kinds of errors. I have done this in standard C, no problem, but as I am new to c++ (especially Builder)I have no idea on how to get this done.

Any ideas or pointers on how to get something like this done?

Thanks!

Johan
 
I managed to solve this one myself using __closure:

typedef struct
{
int iBit;
void (__closure *save_fun)(void);
} __save_rec;

and keeping the rest as is.

Now to figure out mass initialisation as follows (in standard c:)

const __save_rec SaveData[] =
{
{ 1, TRBMAP, SavBmap },
{ 2, TRPAN, SavPan },
{ 3, TRRSP.TRPROC, GenSav },
{ 4, TRRSP.TRTOTAM, SavAmt },
...
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top