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.
#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;
}
Also see: