Using an Array of Functions

Click here to change the theme.

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>


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 *);
    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)

int main(int argc, char *argv[], char *envp[]) {
    CDocument Document;
Document.DoImport(1, "whatever");
Document.DoImport(9, "invalid");
return 0;

Also see: