-
December 13th, 2009, 11:17 PM
#1
dynamic array of delegate void methods
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>();
commandarray.Add(hello(internalcmd, initialdata));
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.
i dont know what would be suit me. i know c++/clr has delegates but wouldnt it best suit me to use native c++ code.
thanks
-
December 13th, 2009, 11:28 PM
#2
Re: dynamic array of delegate void methods
Originally Posted by projectnz
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.
Regards,
Paul McKenzie
-
December 13th, 2009, 11:28 PM
#3
Re: dynamic array of delegate void methods
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
Code:
class VoidFunction
{
class VoidFunctionBase
{
public:
virtual void operator() = 0;
};
template <typename T>
class VoidFunctionImpl
{
T func;
public:
VoidFunctionImpl(T f): func(f) {}
void operator() {func();}
};
VoidFunctionBase *fptr;
// Uncopyable
VoidFunction(const VoidFunction &orig);
void operator=(const VoidFunction &rhs);
public:
template <typename T>
VoidFunction(T func): fptr(new VoidFunctionImpl(func)) {}
~VoidFunction() {delete fptr;}
void operator() { (*fptr)(); }
};
(untested)
-
December 14th, 2009, 12:11 AM
#4
Re: dynamic array of delegate void methods
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?
thanks for replies
-
December 14th, 2009, 12:40 AM
#5
Re: dynamic array of delegate void methods
C++ doesn't have a built in facility for creating an array of various types.
C# and Java can do this easily because every object inherits from some kind of base object. That is not the case in C++.
As Lindley suggests, look into templates and function pointers. The delegate key word is specific to C# and doesn't have an equivalent in C++
Cheers
-
December 14th, 2009, 01:02 AM
#6
Re: dynamic array of delegate void methods
does this seem right, it seems to compile right
vector<void (*)()> functions;
now all i need to do is add a method to functions array?
-
December 14th, 2009, 01:14 AM
#7
Re: dynamic array of delegate void methods
Originally Posted by projectnz
does this seem right, it seems to compile right
vector<void (*)()> functions;
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:
Code:
typedef void (*Function)();
// ...
vector<Function> functions;
-
December 14th, 2009, 01:25 AM
#8
Re: dynamic array of delegate void methods
Your original code may be translated to C++ by the following way:
public delegate void Action(List<int> internalcmd, List<byte> initialdata);
public List<Action> commandarray = new List<Action>();
typedef void (*Function)(std::vector<int>& internalcmd, std::vector<insigned char>& initialdata);
std::vector<Function> functions;
functions.push_back(hello);
-
December 14th, 2009, 02:00 AM
#9
Re: dynamic array of delegate void methods
sorry to be a nuisance but if i compile the following doesnt work
#pragma once
# include <vector>
using namespace std;
class Command
{
private:
typedef void (*Function)();
vector<Function> functions;
void Cleanup();
public:
void Run();
};
#include "Command.h"
void Command::Run()
{
functions.push_back(Cleanup);
}
void Command::Cleanup()
{
}
this does not work comes back with a error
Error 1 error C3867: 'Command::Cleanup': function call missing argument list; use '&Command::Cleanup' to create a pointer to member
-
December 14th, 2009, 02:04 AM
#10
Re: dynamic array of delegate void methods
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.
-
December 14th, 2009, 03:07 AM
#11
Re: dynamic array of delegate void methods
typedef void (Command::*Function)();
...
functions.push_back(&Command::Cleanup);
To execute function by pointer:
Function f = functions[0];
(this->*f)();
-
December 14th, 2009, 03:21 AM
#12
Re: dynamic array of delegate void methods
awesome thanks big help
-
December 14th, 2009, 09:15 AM
#13
Re: dynamic array of delegate void methods
Originally Posted by laserlight
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|