Solved

delegates and functions

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
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…

732 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