Hi, I am trying to do the following:
Save.h:
typedef struct foo
{
unsigned saveno;
void (*save_fn)(void);
} save_rec;
save_rec *pSaveRec;
class TSmartHost : public TForm
{
...
private:
...
void save_Data1(void);
void save_Data2(void);
...
public:
__fastcall TForm1(TComponent* Owner)
{
save_rec Save_Tab[2] =
{
{1, save_Data1(void) },
{2, save_Data2(void) }
};
}
...
};
so that I can do this in the main app:
Save_Record(int iRecVal)
for(int i=0;i<10;i++)
{
if(iRecVal==i)
{
pSaveRec=Save_Tab[i];
pSaveRec->save_fn();
}
}
I am not so hot in c++, and got this working in standard c, but for my new threaded project I need to do this in c++, but I am not having major success. Any help or ideas will be appreciated!
Thanks
Johan
Assigning functions to structure in array
Started by jerlank, Apr 18 2011 08:58 AM
1 reply to this topic
#1
Posted 18 April 2011 - 08:58 AM
#2
Posted 18 April 2011 - 09:20 AM
First, use
But I suggest you start learning C++ and OOP, rather than using a C++ compiler to compile C code, which is what you're doing know. I rarely need to use function pointers in C++.
...[[i][/i]/code] tags to post code.
Secondly, this isn't possible. Your methods need a 'this' pointer, and save_fn is just a regular function. So save_Data1, which is a void (TSmartHost::*)() can't be converted to a void(*)().
You could try using std::tr1::function to store the function "pointer" and std::tr1::bind to bind the 'this' pointer, but I don't know whether they're available for the compiler you're using (Borland?).
[code]#include <functional>
struct save_rec
{
unsigned saveno;
std::tr1::function<void()> save_fn;
};
save_rec Save_Tab[2] =
{
{1, std::tr1::bind(&save_Data1, this) },
{2, std::tr1::bind(&save_Data2, this) }
};
But I suggest you start learning C++ and OOP, rather than using a C++ compiler to compile C code, which is what you're doing know. I rarely need to use function pointers in C++.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.
-
Currently working on: the 3D engine for Tomb Raider.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











