Jump to content
  • Advertisement
Sign in to follow this  
GanonPhantom

Delegates .NET only?

This topic is 3690 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!