1

I was looking through the source code of a project called CrossWindow-Graphics today when I saw an interesting function declaration. The code seemed to be declared as a function pointer. Here is a similar example I wrote.

#include <iostream>
using namespace std;

void (*Test())
{
    cout << "Test Called!" << endl;
}

int main()
{
     Test();
     return 0;
}

My question is why does C++ allow this. I'd imagine it has something to do with function pointers. I remember that typedef is really just creating a variable and the name of the variable becomes the new type so I suppose this is just the same as creating a "variable" but it points to code. Even if that is the right idea, What is the purpose of that?

James51332
  • 164
  • 11
  • In modern C++ you have [std::function](https://en.cppreference.com/w/cpp/utility/functional) and [lambda expressions](https://en.cppreference.com/w/cpp/language/lambda). Notice that graphical toolkits like [Qt](https://qt.io/) or [FLTK](https://fltk.org/) use them a lot – Basile Starynkevitch Feb 06 '21 at 06:24
  • 1
    Look like an unnecessary parentheses. There are https://stackoverflow.com/questions/45746922/why-are-these-function-names-in-parenthesis https://stackoverflow.com/questions/6693970/what-does-an-asterisk-before-a-function-name-mean?noredirect=1&lq=1 https://stackoverflow.com/questions/13600790/what-do-the-parentheses-around-a-function-name-mean/13600837 but they're different things. – user202729 Feb 06 '21 at 06:28
  • there's https://cdecl.org/ so... – user202729 Feb 06 '21 at 06:29
  • @user202729 I just copied the function into cdecl but It actually gave me an error even though the code compiles without any warnings. – James51332 Feb 06 '21 at 06:41
  • @James that website is for declarations. Put it as `void (*Test());` and it works. – Aykhan Hagverdili Feb 06 '21 at 06:46
  • @James51332 This sounds like you saw a `void (*Test)();` declared somewhere, but then typed some completely different code into the question. – dxiv Feb 06 '21 at 07:05
  • @dxiv I literally am so stupid. Thank you for pointing that out. It was a ```void (*Test)();``` – James51332 Feb 06 '21 at 07:22
  • @James51332 One more reason to copy/paste the real code next time. That defines a pointer-to-function, indeed, which you can assign with `GetProcAddress` then call as `Test();`. – dxiv Feb 06 '21 at 07:41

1 Answers1

1

All you've really done here is add an extra pair of () that doesn't do anything. We could write the same functin as

void* Test() { ... }

It's a function that returns a void*.

So the function should have a return statements where it returns a void pointer. Some compilers will warn about this as seen here.

super
  • 12,335
  • 2
  • 19
  • 29
  • That's really interesting. In the library I was looking at it was used as a getProcAddress() method so I should've seen that coming but they might serve to suppress some warning of casting a proc-address to a void*. I have had to fiddle with that to get around meaningless warnings when loading function addresses before. – James51332 Feb 06 '21 at 06:45