Skip to content

Declaring a pointer to a function in C++

Question

Show how to declare a variable that points to a function in C++.

Short answer

double (*f)(double); declares f as a pointer to a function taking a double and returning a double; assign with f = &std::exp; and call as f(1.0). The brackets round *f matter. Aliases help (using Fn = double(*)(double);), and a pointer to a member function looks like double (A::*pm)(double) const;.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.8, p.226; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question