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
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>
usingnamespace 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;
}
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?
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?