Solved

delegates and functions

Posted on 2011-02-21
7
843 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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 columnThat will then direct you to their download page.From that page s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

786 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