I've got an assessment to give to my students.

It's about C++ and function pointers.

Their skill is middle: it the first year of a programming school after bachelor.

To give you something precise, here's a sample of a solution of one of 3 exercices they had to do in 30 minutes (the question was: "here's a version of a code that could be written with function pointers, write down the same thing but with function pointers"):

typedef void (*fcPtr) (istream &);
fcPtr ArrayFct [] = { Delete , Insert, Swap, Move };

void HandleCmd (const string && Cmd)
    string AvalaibleCommands ("DISM");
    string::size_type Pos;
    istringstream Flux (Cmd);

    char CodeOp;
    Flux >> CodeOp;

    Pos = AvalaibleCommands.find (toupper (CodeOp));

    if (Pos != string::npos) {
        ArrayFct [Pos](Flux);

Any idea where I could find some inspiration?

Some of the students have understood the principles, even though it's very hard for them to write C++ code. I know them, I know they're clever, and I'm pretty sure they should be very good project managers. So, writing C++ code is not that important after all. Understanding is the most important part (IMHO).

I'm wondering about maybe break the habits, and give half of the questions about the principle, or even better, give some sample in other language and ask them why it's better to use function pointers instead of classical programming (usually a big switch case).

Any idea where I could look? Find some inspiration?

  • How about you provide them with two different sorting algorithms - and get them to design a class which can utilize either of them to sort a data set (eg the strategy pattern)
    – MattDavey
    Mar 27, 2012 at 15:34
  • And the assignment is? Mar 27, 2012 at 15:49
  • 1
    Also, if it is not important for them to learn c++, why does the class teach it? If they are required to do it, but you think it is not important, then give them simpler assignment where they have to apply the principle. Do not do smart things like above (convert std::string to char, then to upper case and then searching in available commands) Mar 27, 2012 at 16:00

1 Answer 1


My immediate reaction would be to sit back and think twice about doing this at all. Pointers to functions aren't exactly the most commonly used feature in C. At least 95% of the time that you'd use a pointer to a function in C, you'd be better off using a virtual function in C++.

What you're left with is such a tiny amount of legitimate usage that there's just not much point in teaching it at all unless you've covered every other detail of the language in so much detail that there's just nothing else left to cover (which, after only 4 years of classes strikes me as highly unlikely).

  • In Windows C and C++ programming one often uses function points to call functions from a dll that is defined as a C header file in cases where you want to dynamically load the dll. Mar 27, 2012 at 16:44
  • @JimInTexas: Okay, fair enough (and the same applies with dlopen/dlsym on POSIX-like systems). I think if I were you I'd add an expanded version of that as an answer. Mar 27, 2012 at 16:46
  • @JerryCoffin Please read the answer of this question programmers.stackexchange.com/questions/141329/… then maybe there's a nice answer to "why student must understand what pointers to function are, and how to use them wisely". Mar 28, 2012 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.