in C++ by (14.2k points)
0 like 0 dislike
33 views
How I can to have miscellaneous kind of function pointers (different return types and arguments) in a single vector with function pointers C++?

1 Answer

0 like 0 dislike
by (14.2k points)

For different parameters you can pass the parameters want as the second parameter and use stdargs.h to retrieve them. Also you can using templates for this.

C++ stdarg example:

#include <iostream>
#include <vector>
#include <string>
#include <stdarg.h>

void show(int n, ...) {
	std::cout << "show:";
	int val;
	va_list vl;
	va_start(vl, n);
	for (int i = 0; i < n; i++) {
		val = va_arg(vl, int);
		std::cout << " " << val;
	}
	va_end(vl);
	std::cout << std::endl;
}

int main() {
	show(2, 1, 2);
	show(4, 1, 2, 3, 4);
	
	std::vector<void (*)(int, ...)> fns;

	fns.push_back(show);
	fns.push_back(&show);

	fns[0](2, 1, 2);
	fns[1](4, 1, 2, 3, 4);

	(*fns[0])(3, 1, 2, 3);
	(*fns[1])(1, 1);
	
	return 0;
}

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.