Consider this simple program:
#include <iostream>
void length(void){
std::cout<<"My length is void"<<std::endl;
}
void width(void){
std::cout<<"My width is void"<<std::endl;
}
int main(void){
std::cout<<"The program length is: "<<length<<std::endl;
std::cout<<"The program width is: "<<width<<std::endl;
}
The output of this program is:
The program length is: 1
The program width is: 1
What are the numbers that the program prints out, and basically not having much knowledge in C++ particularly, this looks more pythonic syntax to me,and looks as if it should print the function address. I say this because, this problem arose initially while practicing some very basic programs for openGL,
The callback was registered in GLUT with something like:
void line(void){
//some code for line program ...
}
int main(int argc, char** argv){
//some more code;
glutDisplayFunc(line);
glutMainLoop();
}
It's almost seeming as if we're passing the address of the function, but, clearly from the above program this is not an address, and the syntax for pointer to function is a bit different, if so, how is this function being registered for callback? And what is it that we're passing to glutDisplayFunc
?
and, since I **wanted to register a function that had passed arguments, I search a C++ analogy for python lambda functions, and found similar lambda function, but it didn't work out: **
#include <iostream>
void line(int a, int b){
//some code that does some plotting in the display window
}
int main(int argc, char** argv){
auto callback = [](void)->void{
line(a,b);
};
glutDisplayFunc(callback);
glutMainLoop();
}
This totally fails,the error that is shown is:
no suitable conversion function from "lambda []void ()->void" to "void (*)()" exists
This was using my python analogy, but what's the solution to this problem? The major parts of confusion are highlighted in bold.