Should we use p(..) or (*p)(..) when p is a function pointer?

Posted by q0987 on Stack Overflow See other posts from Stack Overflow or by q0987
Published on 2011-11-22T17:46:24Z Indexed on 2011/11/22 17:50 UTC
Read the original article Hit count: 143

Filed under:

Reference: [33.11] Can I convert a pointer-to-function to a void*?

#include "stdafx.h"
#include <iostream>

int f(char x, int y) { return x; }
int g(char x, int y) { return y; }

typedef int(*FunctPtr)(char,int);

int callit(FunctPtr p, char x, int y)  // original
{
    return p(x, y);
}

int callitB(FunctPtr p, char x, int y) // updated
{
    return (*p)(x, y);
}

int _tmain(int argc, _TCHAR* argv[])
{
    FunctPtr p = g;                    // original
    std::cout << p('c', 'a') << std::endl;

    FunctPtr pB = &g;                  // updated
    std::cout << (*pB)('c', 'a') << std::endl;

    return 0;
}

Question> Which way, the original or updated, is the recommended method?

Thank you

Although I do see the following usage in the original post:

 void baz()
 {
   FredMemFn p = &Fred::f;  ? declare a member-function pointer
   ...
 }

© Stack Overflow or respective owner

Related posts about c++