What does the syntax &function_name mean?
For example, as used in the delegate
System::ComponentModel::DoWorkEventHandler(this, &Form1::DoWorkEvent)
as used with event handling with a BackgroundWorker object.
The delegate prototype is
public delegate void DoWorkEventHandler(Object^ sender, DoWorkEventArgs^ e)
Presumably this means that &Form1::DoWorkEvent returns a DoWorkEventArgs object? But that doesn’t make much sense.
System::ComponentModel::DoWorkEventHandler(this, &Form1::DoWorkEvent)creates a delegate. The syntax for creating a delegate isgcnew delegatename(thisObject, functionPointer). It creates a delegate that callsfunctionPointerwiththisset tothisObject.To invoke a delegate, you call it with the appropriate parameters.
delegateVariable->Invoke(parameter1, parameter2, etc). In the case ofDoWorkEventHandler,parameter1issenderandparameter2ise.You’re confusing two different things, creation and invocation. The line
DoWorkEventHandler(this, &Form1::DoWorkEvent)creates a delegate. The two parameters arethisObjectandfunctionPointer, notsenderande.This should have been covered by whatever book/resource you used to learn about delegates in the first place. For example, this article says