This is the type of thing that you are not likely to use and if you need it you will not remember where you saw it. In case this will be of use to someone, though, the following is a sample of creating and using an array of functions; actually, an array of function pointers. Using this technique, you can have specialized routines for importing fields to a class and exporting fields from a class.
I have also found a C/C++ Users Journal article that appears to also be relevant; see "Questions and Answers: Sep98" for a description of how to use tables of function pointers. Also see MS KB article Q94579 - INFO: Creating a Function Pointer to a C++ Member Function.
#include <iostream.h>
#define NUMBEROFFIELDS 3
class CDocument {
void ImportName(char *s) {cout << "Importing name: " << s << endl;};
void ImportPosition(char *s) {cout << "Importing position: " << s << endl;};
void ImportSize(char *s) {cout << "Importing size: " << s << endl;};
void (CDocument::*ImportField[NUMBEROFFIELDS])(char *);
public:
CDocument();
void DoImport(int x, char *s);
};
CDocument::CDocument() {
ImportField[0] = ImportName;
ImportField[1] = ImportPosition;
ImportField[2] = ImportSize;
}
void CDocument::DoImport(int x, char *s) {
if (x>=0 && x<NUMBEROFFIELDS)
(this->*ImportField[x])(s);
}
int main(int argc, char *argv[], char *envp[]) {
CDocument Document;
Document.DoImport(1, "whatever");
Document.DoImport(9, "invalid");
return 0;
}
See my Visual C++ Programmer Stuff page for more C++ stuff.