I am in the process of trying to gain a clear understanding of what a callback is. I came across this post: what-is-a-callback-function. The user 8bitjunkie who answered the question mentioned callbacks are named such because of how they are used in pointer languages. My initial assumption based on the name led me to think that a pointer language is a language where pointers can be directly manipulated. So I would like to know if c++ is a pointer language, and if my initial assumption was incorrect; what a pointer language is. As far as I can tell it does not seem to be a typical language agnostic term. If it is, it is covered by results relating to the usage of pointers.
-
Im not sure how to tag this as it doesn't directly relate to c++, pointers or callbacks. I am simply trying to track down an example similar to what 8bitjunkie was thinking of when he mentioned pointer languages. I would message him on stack exchange if I knew how. – Josh Nabours Dec 18 '15 at 21:11
-
a pointer is just a variable which contains a memory address of some OTHER bit of data. a callback pointer would simply be the memory address of where this call back function's code exists in memory, and is a place for the cpu to "jump" to when calling/executing the function. that's all. – Marc B Dec 18 '15 at 21:14
-
I don't know why people are down voting this. There is really nothing wrong with this question. – William Rosenbloom Dec 18 '15 at 21:18
-
Oh. so then I was just making a bigger deal out of the term than was necessary. – Josh Nabours Dec 18 '15 at 21:21
-
Marc B - So Would it be correct to say that the source of the naming for the term callback comes from the fact that a callback pointer moves the program counter back to a previous spot in memory to call the function in question? – Josh Nabours Dec 18 '15 at 21:27
1 Answers
Callbacks are not unique to languages that allow direct manipulation of pointers - but that is what a "Pointer Language" is. I will focus my answer on what callbacks are because that seems to be your main confusion.
Callbacks are available in Java, Python, JavaScript, and many other languages that hide pointers from you.
A callback is just a function that will be executed at the end of another function. Generally this is useful for asynchronous tasks, because it allows you to respond to the task in a specific way without blocking.
For an example I will use Java - a language with managed memory no direct access to pointers. The more native way to implement callbacks is with function pointers, and I think that is what your article meant about "Pointer Languages." But I'd rather show you what a callback is and how to use them without pointers in one fell swoop, so Java it is.
In this example we will have an interface defined like this.
public interface CallBack {
public void onFinished(boolean success);
}
This callback interface allows us to declare an object with a predefined method that will respond to either success or failure. We can then define a Runnable class like this.
public class CBObject implements Runnable {
private CallBack myCallback;
public CBObject(CallBack myCallback) {
this.myCallback = myCallback;
}
public void run() {
boolean success = false;
// do some stuff, set success = true if it works
myCallback.onFinished(success); // this calls the callback
}
}
Then if we want to use this callback we will do something like this.
public void doSomethingAsynchronous(CallBack callback) {
CBObject cb = new CBObject(callback);
Thread task = new Thread(cb);
task.start();
}
This will run this task asynchronously but allow the user to react to its success or failure.
I hope this helps!

- 2,506
- 1
- 14
- 37
-
So it does seem that my initial assumption is correct and c++ is a pointer language. Good to know. As for your example; I understand how each of the individual methods and such work because I have a small amount of experience with threading in java, but I can't figure out how the run method would be called. In particular I am having trouble wrapping my mind around how each method and section would lead into the others. Could you possibly provide a step by step run-through as if you were stepping into each of the major portions in an IDE? – Josh Nabours Dec 18 '15 at 22:31
-
@JoshNabours Yes I can but first I'd like to try clearing up your misunderstanding more briefly. The `run` method is called within `Thread task` when the thread's `start` method is called. That's just part of how Java threads work. You initialize them with a `Runnable` and then the thread `run`s that when it starts. Does that help? – William Rosenbloom Dec 18 '15 at 22:36
-
Would the run method happen to be an abstract method required by the runnable interface? – Josh Nabours Dec 18 '15 at 22:59
-
-
Ok. Then it all makes sense now. The callback sort of completes the circuit and allows you to trigger something after all the work is done just like in the fast food worker explanation I read in another stack overflow article. Thank you very much. I appreciate you taking the time to help me understand. – Josh Nabours Dec 18 '15 at 23:04
-
@JoshNabours No problem! Happy to help! Perhaps you wouldn't mind accepting my answer? Haha – William Rosenbloom Dec 18 '15 at 23:07