dynamic array of delegate void methods
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: dynamic array of delegate void methods

  1. #1
    Join Date
    Dec 2009
    Posts
    37

    Exclamation 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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: dynamic array of delegate void methods

    Quote Originally Posted by projectnz View Post
    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    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)

  4. #4
    Join Date
    Dec 2009
    Posts
    37

    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

  5. #5
    Join Date
    Aug 2006
    Posts
    157

    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

  6. #6
    Join Date
    Dec 2009
    Posts
    37

    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?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: dynamic array of delegate void methods

    Quote 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;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Jul 2002
    Posts
    2,526

    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);

  9. #9
    Join Date
    Dec 2009
    Posts
    37

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Jul 2002
    Posts
    2,526

    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)();

  12. #12
    Join Date
    Dec 2009
    Posts
    37

    Re: dynamic array of delegate void methods

    awesome thanks big help

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: dynamic array of delegate void methods

    Quote Originally Posted by laserlight View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center