prog9_05.cpp

来自「一本语言类编程书籍」· C++ 代码 · 共 34 行

CPP
34
字号
// Program 9.5 Exercising pointers to functions
#include <iostream>
using std::cout;
using std::endl;

long sum(long a, long b);               // Function prototype
long product(long a, long b);           // Function prototype

int main() {
  long (*pDo_it)(long, long) = 0;       // Pointer to function declaration

  pDo_it = product;
  cout << endl
       << "3*5 = " << pDo_it(3, 5);      // Call product through a pointer

  pDo_it = sum;                         // Reassign pointer to sum()
  cout << endl
       << "3 * (4+5) + 6 = " 
       << pDo_it(product(3, pDo_it(4, 5)), 6);  // Call thru a pointer twice

  cout << endl;
  return 0;
}

// Function to multiply two values
long product(long a, long b) {
  return a*b;
}

// Function to add two values
long sum(long a, long b) {
  return a+b;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?