hi im new to c++ and i done a few project's in c# java VB etc and im trying to work out how to get an dynamic array of delegate void methods i have some code in c# to explain what im trying to do and some how convert it to unmanaged c++.
public delegate void Action(List<int> internalcmd, List<byte> initialdata);
public List<Action> commandarray = new List<Action>();
hi im new to c++ and i done a few project's in c# java VB etc and im trying to work out how to get an dynamic array
Code:
std::vector
std::list
iv done a little research and you can use vectors instead of list array. but im not too sure about how to use a delegate or function pointer.
What is a "delegate"?
I think you should concentrate on what your application is supposed to do, and rewrite it using C++ instead of trying to translate line-by-line from C# to C++. Doing a line-by-line translation will only get you into trouble, since the languages are not the same and the similar keywords such as "new" do different things and are supposed to be used differently.
A function pointer would do it, but might not be the most flexible solution; it would only accept actual functions, and not functors (which are often more useful).
One means of handling either a function pointer or a functor transparently is to use a template type to represent the thing. However, for a container such as a std::vector, this isn't an option since every element of the vector must have the same type.
If you have access to Boost, then the boost::function library provides a solution to the problem. If not, you can probably duplicate that functionality to a limited extent without too much trouble, something like
to the first replier i meant to say "how to make a dynamic array of functions".
i know that vector array has to have a generic type like int, char etc. but what about the list array im sure thats dynamic and doesn't need a generic type?
If you intend to have a vector of pointers to functions that take no arguments and return void, then yes, though it may be easier to read with a typedef:
Unfortunately, a pointer to a member function is not a pointer to a function. You would need a free function, not a member function, going by what you have now.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Unfortunately, a pointer to a member function is not a pointer to a function. You would need a free function, not a member function, going by what you have now.
boost::bind offers one means of getting around this, although as I said, it creates a functor rather than a function and thus would need something more than a simple function pointer to store it.
Bookmarks