List of functions in vector

Jan 10, 2012 at 1:10am
Hi, my problem are, I want to store an array of functions in vector and when I access some position of my array, need that function will be called. Anyone have some hint to help me =)
I've been thinking some like:
vector<void f(*pt2Func)(void*)> functions
Jan 10, 2012 at 1:26am
Try:
 
std::vector<void(*)(void*)> functions;
Jan 10, 2012 at 1:29am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

void foo(void*)
{
	std::cout << "foo" << std::endl;
}

void bar(void*)
{
	std::cout << "bar" << std::endl;
}

int main()
{
	std::vector<void(*)(void*)> functions;
	functions.push_back(foo);
	functions.push_back(bar);
	
	functions[0](NULL); // this will call foo(NULL)
	functions[1](NULL); // this will call bar(NULL)
}

Jan 10, 2012 at 1:38am
1
2
typedef void (*foo)(void*); //just to make it clear
std::vector<foo> functions;
Jan 10, 2012 at 5:53pm
Ok, it works fine, but now I want a little more, some think like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Methods.h
#pragma once
class Methods
{
public:
	Methods(void);
	~Methods(void);

	void methodOne();
	int  methodTwo();

	void methodTree(int value);
	int	 metohdFour(int value);
	int	 metohdFive(int* value);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Methods.cpp
#include "Methods.h"
#include<iostream>

using namespace std;

Methods::Methods(void){}

Methods::~Methods(void){}

void Methods::methodOne(){cout << "Methods:methodOne" << endl;}
int  Methods::methodTwo(){cout << "Methods:methodTwo" << endl;}
void Methods::methodTree(int value){cout  << "Methods:methodTree" << "Value:" << value << endl;}
int	 Methods::metohdFour(int value){cout  << "Methods:metohdFour" << "Value:" << value << endl; return value*2;}
int	 Methods::metohdFive(int* value){cout << "Methods:metohdFive" << "Value:" << value << endl;}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//main.cpp
#include "Methods.h"
#include <vector>
#include<iostream>

void main(){

	Methods* methods = new Methods();

	std::vector<void(*)(void)> arrayMethods;//How to define here
	
	arrayMethods.push_back(methods->methodOne);//to use this way?

	delete methods;
}
Jan 10, 2012 at 6:01pm
Methods methods; That is how you construct an object.
You would need an object in order to execute the methods (the implicit this parameter), however there is a bigger issue.

¿How are you planning on execute them? ¿How you will know the type and number of parameters that they need?
Jan 10, 2012 at 6:10pm
About the parameters don't worry, because I stored in other place, I just not understand the sintax to construct the vector using methods from some class. To make easy pretend all my methods in class Methods be : void foo(void);, so how I make my vector?
Jan 10, 2012 at 6:14pm
1
2
3
std::vector<void(Methods::*)()> arrayMethods;
arrayMethods.push_back(&Methods::methodOne);
(methods->*arrayMethods[0])(); // this calls the function 
Jan 10, 2012 at 6:17pm
Try:
1
2
3
4
5
6
7
	Methods* methods = new Methods();

	std::vector<void(Methods::*)(void)> arrayMethods;

	arrayMethods.push_back(&Methods::methodOne);

	delete methods;
Last edited on Jan 10, 2012 at 6:17pm
Jan 10, 2012 at 6:20pm
Ok, It was I'm looking for, so thanks ! =D
Jan 10, 2012 at 9:19pm
About the parameters don't worry, because I stored in other place
I'm curious about that. ¿Can you show it?
Topic archived. No new replies allowed.