1

One more time I got trapped by pointer logic. The following code uses a function typedef tOutFunction. It defines a global variable to this function pointer and a set method.

Why does

SetOutFunction(OutFunction);

and

SetOutFunction(&OutFunction);

both work? How could autoconversion work in this place. Or is one of it wrong and it works by accident?

#include <iostream>

typedef int (*tOutFunction)(const char*);

int OutFunction(const char* out)
{
  std::cout << "OutFunction:" << out << "\n";
  return 10;
}

tOutFunction out__ = 0;

void SetOutFunction(tOutFunction outFunc)
{
  out__ = outFunc;
}

int main()
{
  SetOutFunction(OutFunction);
  std::cout << out__("Without Ref") << "\n";

  SetOutFunction(&OutFunction);
  std::cout << out__("With Ref") << "\n";

  return 0;
}

Also tried it here: http://codepad.org/yfxj2QAo

Totonga
  • 4,236
  • 2
  • 25
  • 31

0 Answers0