Where do you learn basic c++ information? Like representing function pointers

Sep 5, 2018 at 6:47am
The title is not very specific but what I am referring to is stuff like having a function accept a pointer to another function. See below for what I am referring to, like why does this mean accept a pointer to a void function with a void * arg? And where is this documented? (And other random information like this)

Also I typed this example code in the browser so there is probably a few errors.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>

void Work(void * args)
{
 //Assume args is char*
 std::cout << (char*)args << std::endl;

}

//Callable is what I am referring to, why does this represent a function? And what documentation specifies it? 
void DoWork(void (*callable)(void*), void* args)
{
   callable(args);
}

int main()
{ 
   DoWork(Work, (void*)"Some info");
}
Last edited on Sep 5, 2018 at 6:48am
Sep 5, 2018 at 8:13am
First, note that modern C++ has alternatives to raw pointers. For example: https://en.cppreference.com/w/cpp/utility/functional/function
That is among: https://en.cppreference.com/w/cpp/utility/functional

https://en.cppreference.com/w/cpp/language/pointer has section: Pointers to Functions.
Sep 5, 2018 at 9:45am
The official source where all this is documented is the C++ standard. It's unfortunately not freely available, but in practice that doesn't matter because it's easy to find publicly available drafts that is as good as identical. The standard isn't meant to be learning material but rather for compiler and standard library implementers. For more digestive information you'll have to look elsewhere. E.g. books, courses, websites, blog posts, etc. The page that keskiverto linked to (cppreference.com) is great for looking things up, but it's rather technical so I'm guessing it can be confusing to beginners (even I get confused sometimes).
Sep 5, 2018 at 10:38am
closed account (367kGNh0)
This is free and what I used. not this very link but solo-learn has a full free course, u can get the app on your phone and go to the module, arrays and pointers.

https://code.sololearn.com/cLxH5h7oX2We/#cpp
Sep 5, 2018 at 2:32pm
TrevinLC1997 wrote:
//Callable is what I am referring to, why does this represent a function? And what documentation specifies it?
void DoWork(void (*callable)(void*), void* args)

technically, keskiverto's https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions doesn't answer that: it explains what function pointers do, not why this particular syntax is used.

You can find that under https://en.cppreference.com/w/cpp/language/declarations#Declarators though it may indeed be a little too dense for someone unfamiliar with C type naming (C++ got this from C).

Do a search for "reading c type declaration syntax" or, more specifically, "c function pointer syntax", e.g. https://stackoverflow.com/questions/14114749/c-function-pointer-syntax
Last edited on Sep 5, 2018 at 2:34pm
Sep 5, 2018 at 4:56pm
if it helps, the name of the function IS a pointer, in the same sense that the name of an array IS a pointer. (technically, I guess they are const pointers since you can't (or should not!!) mess with them). You see that in your example, but its easy to miss. This helps because most uses of function pointers don't use * syntax, they just use the names... I know they are useful in C++, but I havent had the need.
Sep 5, 2018 at 8:09pm
the name of an array IS a pointer. (technically, I guess they are const pointers

Technically they are nothing at all like pointers, const or otherwise. It's like saying "the name of a double IS an int (technically a const int)". An implicit conversion is not an "IS a" relationship.
Sep 6, 2018 at 1:58pm
Fair enough. But the result of the unseen conversion (for function*) is a, and its very handy to let the compiler do this for you when dealing with them.
Topic archived. No new replies allowed.