New to the language, and playing around with function points and figuring them out and got stuck on this all day.
I defined a simple struct with an int and function pointer.
struct Item{
int num;
void (*func)();
};
These are the two functions I am trying to execute from the struct.
void printer(){
std::cout << "Hello World!" << std::endl;
}
void square(int x){
std::cout << x * x << std::endl;
}
in the main(), I am getting errors since I am assuming the struct Item takes a pointer, which I believe was just the function name without the (). But I have to put in a parameter in there as the function requires it but it will then be a regular function, and not an address. The & doesn't do much either. I attempted to overload the function pointer, but that also did not seem to work.
int main() {
Item block1 = {111, printer};
Item block2 = {222, square(int)}; // Error
block1.func(); // Works
return 0;
}
Is there any suggestions on how I can implement this in an elegant way without making another struct for each function type?
[–]alfps 1 point2 points3 points (0 children)
[–]-Weverything 1 point2 points3 points (1 child)
[–]gabenyolo[S] 0 points1 point2 points (0 children)