본문 바로가기

Native/C++

pointers to functions

[code]
// Ex5_01.cpp
// Exercising pointers to functions
#include <iostream>
using namespace std;

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

int main()
{
    // Pointer to function declaration
    long (*pdo_it)(long, long);
    pdo_it = product;
    cout << endl
        // Call product thru a pointer
        << "3*5 = " << pdo_it(3, 5);
    
    pdo_it = sum;                     // Reassign pointer to sum()
    cout << endl
        << "3*(4+5) + 6 = "
        // Call thru a pointer twice
        << pdo_it(product(3, pdo_it(4, 5)), 6);
    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;
}
[/code]

'Native > C++' 카테고리의 다른 글

유니코드로 한글 쓰기 (write korean word Unicode)  (0) 2013.10.02
cout flags  (0) 2013.10.02
Returning a reference  (0) 2013.10.02
templates  (0) 2013.10.02
exception, try, catch, throw  (0) 2013.10.02