?
Solved

delegates and functions

Posted on 2011-02-21
7
Medium Priority
?
878 Views
Last Modified: 2013-12-14
Hi,

I'm playing round with function pointers and delegates, but I can't seem to get a grip on how to do this. I want to pass a function as a parameter to a function. Can some one help me out?

Regards
Eimund Smestad
delegate void del(int);

void Test2(del^ func) { del^ hei = gcnew del(func) }

ref class heisan {
public:
  void Test(int i) {}
  void myFunc() {
    Test2(&heisan::Test);  
    // I want to pass Test before making it a delegate
   }
};

Open in new window

0
Comment
Question by:TheEimund
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 35186853
Here's a very simple example in C (not C++ because I avoid C++ as much as I can). However the way in C++ is the same as in C.

Regards
Friedrich
#include <stdio.h>


typedef int (*prot_fun)(void);


int my_fun(void) {
  return 1;
}


void call_prot_fun(prot_fun a_fun) {

  printf("calling a_fun ... result: %d\n" , a_fun());
  
}


int main(void){
  call_prot_fun(my_fun);
  return 0;
}


output:
 ./a.out
./a.out
calling a_fun ... result: 1

Open in new window

0
 

Accepted Solution

by:
TheEimund earned 0 total points
ID: 35195126
Thank you for your response fridom. I wish it was that simple in managed C++ (Visual C++). One has to use this delegate operator to create a pointer to managed function.

However found my solution to the problem, I forgot about this post. The solution took som effort to make out, but I will give a short summary. First I made a helper class Delegate to help me with the syntax I wanted, were I used a typecast operator to return the adress to the function. I even dropped the referance operator &, I get the adress just by typing the function name.

 
template<class I, class O> ref class Delegate abstract {
	public: typedef I Input;
	public: typedef O Output;
	public: delegate O del(I);
	protected: del^ function;
	public:	operator del^() {
		return function;
	}
};

Open in new window


Next step was to make a function class Method, which is inherited from this Delegate class, which helps me to define my new type of functions with has the syntax I wanted

 
template<class I, class O> ref class Method abstract : Delegate<I, O> {
	protected: virtual O function(I) abstract;
	public:	Method() {
		Delegate::function = gcnew del(this, &Method::function);
	}
	public:	Method(const Method<I, O>% method) {
	        *this = method;
	}
	public:	O operator()(I i) {
		return function(i);
	};
};

Open in new window


This Method class has a pure virtual function which I must override when I declare my function. So every function I want to declare with this syntax must inherit from the Method class

 
ref struct _Test : Method<int, double> {
        // define my function "Test" here
	virtual Output function(Input i) override { 
		return i+0.5;
	}
} Test;

ref struct _Test2 : Method<Object^, double> {
        // define my function "Test2" here
	virtual Output function(Input i) override {
		return 0.5;
        }
} Test2;

Open in new window


It's a bit more ugly way to define a function, but is OK for me since don't have to write

gcnew del(this, &MyClass::Test);

everytime I want a referance to my function "Test". Which requires me to have access to the delegate declartion of  "del", pointer to the object were the function resides and the class name of the class were the function resides. All this handled by helper classes Delegate and Method, and I simply type

Test

when I want a referance to my function Test. That was what I wanted :-)

Eimund Smestad
0
 

Author Closing Comment

by:TheEimund
ID: 35225509
I found my own answer to the questions
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question