• Advertisement
Sign in to follow this  

Delegates .NET only?

This topic is 3513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Are Delegates a .NET keyword only? Cause im not able to use them in DevC++ and only on a Microsoft compiler. :( If yes, are they just tasks that are pushed into a "tasklist" of the target thread you are trying to invoke? (Kinda like a thread pool, but just with one thread doing all of the tasks from the list instead of multiple) If no, how do they work? Thanks, GanonPhantom

Share this post


Link to post
Share on other sites
Advertisement
Yes, they're part of .NET. They're basically just function pointers. It sounds like you're talking about Events, which are implemented with delegates. Basically an event stores a collection of delegates, and when that event is fired each delegate is called. Typically one object will register a delegate that points to a member function, and that member function will contain code that responds to the event.

Share this post


Link to post
Share on other sites
Yes, System.Delegate is a .NET construct. They are (basically) simple function objects that are usually used with System.MulticastDelegate (sp) that makes them into a list of invocation.

Other bits use them where-ever that function object stuff is required. You'll need to be more specific, since they're not intrinsically tied to any sort of threading business.

Share this post


Link to post
Share on other sites
Ok, thanks for the replies.

Here is my game plan. (and please correct me if im doing something wrong or dont understand)

1) Make a template called Delegate.
2) In my thread class (already constructed), i will use my Event container (also already constructed, and lockless btw :) ).
3) When i want to push a new Delegate event, i construct it and push it into the list (using a base class for the template).
4) At the beginning of each loop in the thread, i fire off all the delegates queued up.

Now, the trick i guess i need to know now, is how to check if i need to invoke or not. I.E. check to see if the thread created the object/variable.

Thanks for the replies so far,
GanonPhantom

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
Yes, they're part of .NET. They're basically just function pointers. It sounds like you're talking about Events, which are implemented with delegates. Basically an event stores a collection of delegates, and when that event is fired each delegate is called. Typically one object will register a delegate that points to a member function, and that member function will contain code that responds to the event.

The big diff is that they are type safe in C#/.NET unlike C++:

In C/C++, the address of a function is just a memory address. This address doesn't carry along any additional information such as the number of parameters expected by the function, the types of these parameters, the function's return value type, and the function's calling convention. In short, C/C++ callback functions are not type-safe.
In the .NET Framework, callback functions are just as useful and as pervasive as they are in unmanaged programming for Windows®. However, the .NET Framework has the added bonus of providing a type-safe mechanism called delegates

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
Ok, thanks for the replies.

Here is my game plan. (and please correct me if im doing something wrong or dont understand)

1) Make a template called Delegate.
2) In my thread class (already constructed), i will use my Event container (also already constructed, and lockless btw :) ).
3) When i want to push a new Delegate event, i construct it and push it into the list (using a base class for the template).
4) At the beginning of each loop in the thread, i fire off all the delegates queued up.

Now, the trick i guess i need to know now, is how to check if i need to invoke or not. I.E. check to see if the thread created the object/variable.

Thanks for the replies so far,
GanonPhantom


Why not just use C#, which has all of that stuff built in?

Share this post


Link to post
Share on other sites
From what i hear, C++ is a faster language then C#, but i dont have any benchmarks to prove that. /me heads for google.

At another note, ive been using C++ for about 2 years now, just kinda used to it, and have gotten used to having to do some things myself. (the unmanaged stuff)

Im going to head to google to find some benchmarks, but if anyone know some sites that have credible benchmarks, i would appreciate the link :).

Edit: I found this, it was at the top of my search. Not sure if these are credible benchmarks but, this is what i found.

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
From what i hear, C++ is a faster language then C#, but i dont have any benchmarks to prove that. /me heads for google.


Performance characteristics are inherently complex and there will be certain situations where you might find either language/platform to have advantages. Any benchmark that shows a conclusive result to such an incredibly broad question should be looked at with a very skeptical eye.

Quote:
Original post by GanonPhantom
At another note, ive been using C++ for about 2 years now, just kinda used to it, and have gotten used to having to do some things myself. (the unmanaged stuff)


As someone who used C++ almost exclusively for years, I can tell you that knowing how to "do things myself" just makes me truly appreciate the built-in features of C# and .NET. [smile]

Share this post


Link to post
Share on other sites
It just seems odd to me when people say "oh, I need support for delegates, events, reflection, and lambdas. How can I make those work in C++?" Why not just use a language that provides all that stuff for free?

Share this post


Link to post
Share on other sites
Another reason i use C++ is for platform independence (which i know C# has C# Mono, but it dosnt have everything yet). I honestly think the language someone uses should be completely up to them (as how they like to write, and what they like to handle themselves), thats why i usually dont try to convert people to the language of my profitability.

That said though, thanks for the replies, as they have helped me alot.

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
Another reason i use C++ is for platform independence (which i know C# has C# Mono, but it dosnt have everything yet).


If I consider the small number of projects that are actually completed, let alone ported to another platform, I find "platform independence" one of the most overrated features of C++. If I have to choose between a program that runs smoothly but exclusively on Windows and a project that is portable in theory but unfinished and/or buggy, my choice is clear :-) It's probably easier to make a C# program portable than it is to build all cool C#/.NET features into C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by GanonPhantom
I honestly think the language someone uses should be completely up to them (as how they like to write, and what they like to handle themselves), thats why i usually dont try to convert people to the language of my profitability.


If you want a completely cross-platform language that supports delegates, use Objective Caml [wink]

I can understand that you don't like being told what language to use. But the easiest way to shut people up is to explain that you don't want to use that language, for reasons outside the realm of intrinsic language properties.

If you start explaining that you wish to use a language instead of another for practical reasons X or Y related to the language, you will find people more experienced than you with both languages which will just tear down your arguments. In this particular situation, your hand-made C++ implementation of delegates will be slower than the compiler-supported C# delegates (and besides, C# is and has always been faster than C++ when using C# idioms, as you seem to be doing), and most of the missing parts in Mono are related to the less-used parts of .NET, which often have no portable C++ equivalent.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
I can understand that you don't like being told what language to use. But the easiest way to shut people up is to explain that you don't want to use that language, for reasons outside the realm of intrinsic language properties.

If you start explaining that you wish to use a language instead of another for practical reasons X or Y related to the language, you will find people more experienced than you with both languages which will just tear down your arguments. In this particular situation, your hand-made C++ implementation of delegates will be slower than the compiler-supported C# delegates (and besides, C# is and has always been faster than C++ when using C# idioms, as you seem to be doing), and most of the missing parts in Mono are related to the less-used parts of .NET, which often have no portable C++ equivalent.


That was extremely well said. Rate++ for you good sir. In fact, that should go in an FAQ or Wiki entry somewhere.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement