+ 1
How to pass a function as an argument to another function in C++?
Hello everyone, I'm trying to pass a function to another function but I'm not sure on how to do that. Here is my code so far: // Example program #include <iostream> #include <string> struct listNode{ int num; listNode * next; }; int numOfNodes(struct listNode firstNode){ int countNodes = 0; while(firstNode -> next != NULL){ countNodes ++; } return countNodes + 1; } int middleOfList(struct listNode firstNode,){ //incomplete What I'm trying to do is pass numOfNodes to middleOfList as an argument. Any answers will appreciated.
3 Respuestas
+ 2
https://en.wikipedia.org/wiki/Function_pointer
But I'm not sure a function pointer would be a very wise idea here.
Not something for a beginner to play around with as the syntax can be quite confusing.
Why not just call numOfNodes inside middleOfList?
+ 2
I honestly wasn't thinking of that. I'm going to do that then, Thank you