Adding star(*) makes no difference. Why?


Please see the comment at the bottom of the code section.

int Func(char c)
{
	printf("Func( %c )\n", c);
	return 0;
}
int (*pFunc)(char c);
int TestFuncPtr()
{
	pFunc = Func;
	(pFunc)('a');
	(*pFunc)('b'); //we add a * here, but it makes no difference
	return 0;
}

pFunc and *pFunc are treated the same. Here is what’s happening:
pFunc is the function pointer. *pFunc’s rvalue is never used, and therefore we have it return the same as pFunc. It’s the similar case with arrays, whose address is returned as its rvalue.
Why is that? My answer is that C++ doesn’t have to do it that way. To prove it, member function pointers are not treated like that. IMO, C++ should treat the non-member function pointers the same way.

Advertisements
Comments are closed.
%d bloggers like this: