?
Solved

delegates and functions

Posted on 2011-02-21
7
Medium Priority
?
890 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
3 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 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