Delegate - what is this?

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I'm trying to write a C++ app using a VB example. It uses the Delegate keyword, but I'm not sure exactly what it is or what the equivalent would be in C++.
Here is the code:
Code:
Application.DoEvents()
While True
  If retCode = 0 Then
    '---either error or stop is requested
    Exit While
  Else
    Dim updateDelegate As New _
        myDelegate(AddressOf updateMessageLog)
    updateDelegate.Invoke(byteArrayToString(inbuff))
    ReDim inbuff(300)
    retCode = ReadFile(infileHandler, _
                       inbuff, _
                       inbuff.Length, _
                       numReadWrite, _
                       0)
    Application.DoEvents()
  End If
End While
The Delegate is defined as follows:
Code:
Public Delegate Sub myDelegate(ByVal str As String)
Public Sub updateMessageLog(ByVal str As String)
  If str.Length > 0 Then
    txtMessageLog.Text += str
  End If
End Sub
While I am asking, does anyone know what Application.DoEvents() and ReDim are and what their equivalents would in C/C++? I would guess Sleep(1) and memcpy, but I would like a confirmation if someone knows this.
 
Last edited:
Managed or native?

Are you trying to port this to managed C++ or native C++? Converting VB.Net to managed C++ is much easier as it is all just a matter of syntax, whereas converting to native C++ requires changing basically everything.

For example, this is how the code might look in managed C++:

(untested, don't take as gospel)
Code:
Application::DoEvents();

while (retCode) {
    myDelegate^ updateDelegate = gcnew myDelegate(this, MyClass::updateMessageLog);
    updateDelegate.Invoke(byteArrayToString(inbuff));

    inbuff = gcnew int[299]; //Or whatever type inbuff is

    retCode = ReadFile(infileHandler, inbuff, inbuff.Length, numReadWrite, 0);
    Application::DoEvents();
}

Converting to native C++ is a whole other kettle of fish. For example, Application.DoEvents() alone would have to be written thus:

(again, untested)
Code:
void DoEvents()
{
    MSG msg;

    ZeroMemory(&msg, sizeof(msg));
    while (PeekMessage(&msg, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

As you can see, native C++ is nothing like managed C++. If you're coding in native C++ then I don't think you should be reading VB.Net examples as the two are very different beasts.

Good luck :cool:
 
Last edited:
Hi again Mr. Paul,

My apologies. I should have stated that I'm using native C++.

I'll try my hand at fabricating a DoEvents procedure similar to what you have pointed out.

Thanks for that, by the way! I did not think it was an important step to consider! I was going to code a Sleep(), and consider it done. I didn't realize it would be so important.

Back to Delegate, though: Is this a CALLBACK function, or something similar?
 
Delegates (at their simplest anyway) are really just typesafe function pointers, therefore under C / C++ there isn't a direct replacement for the delegate keyword as you can just use a normal function pointer.
 
DoEvents, delegates and ReDim

Application.DoEvents()
Thanks for that, by the way! I did not think it was an important step to consider! I was going to code a Sleep(), and consider it done. I didn't realize it would be so important.

The description of Application.DoEvents() reads:

[horizontalrule]-[/horizontalrule]Processes all Windows messages currently in the message queue.
...
Unlike Visual Basic 6.0, the DoEvents method does not call the Thread.Sleep method.

[horizontalrule]-[/horizontalrule]
Of course in your own implementation you may add a call to Sleep() as you wish.

Delegates
As PlausiblyDamp said, delegates are basically type-safe function pointers. However, they are often tied to a specific class instance. C++ does not have this functionality natively, so you'd have to design your own delegate system to reproduce it. For most purposes I expect that regular function pointers would suffice, but it all depends on how closely you are trying to copy the VB code - as I said before, they are very different languages.

ReDim
ReDim resizes an array. It does not preserve the contents of the array unless the Preserve keyword is specified. In the VB code it looks like ReDim is being (mis)used merely to erase the contents of the array. In general you would probably reproduce ReDim using the new operator, although you must remember to use delete at some point to avoid memory leaks. To erase the contents of an array you could use ZeroMemory(). Again, it doesn't translate perfectly without ambiguity.


Not wanting to sound like a broken record, but VB.Net is probably one of the poorest choices to translate samples into native C++ from. The differences between the two languages are so vast that all but the most fundamental algorithmic concepts will be lost or require major rewriting during the translation. I would highly recommend searching for samples in any non-managed language or failing that, C#. It will save you considerable time and effort in the long run.


Good luck :)
 
Back
Top