Solved

delegates and functions

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now