|
|
Home » U++ Library support » U++ Callbacks and Timers » Virtual functions versus callbacks
Virtual functions versus callbacks [message #24202] |
Tue, 05 January 2010 09:59 |
|
koldo
Messages: 3357 Registered: August 2008
|
Senior Veteran |
|
|
Hello all
To let the program do actions after events I have seen there are two options in Upp:
- To use "virtual" functions
For example to get the "enter" key in an EditField it is necessary to do a derived class
that catches virtual bool Key(dword key, int rep);
- To use callback
For example to do something after clicking in an ArrayCtrl it could be done by assigning a callback function to "WhenLeftClick".
From the user (programmer) point of view it seems better callback functions as you do not need to do derived classes every time you need a certain response after a Ctrl event.
Personally I stronger prefer callback but, what do you think about it ?
Best regards
Koldo
Best regards
Iñaki
|
|
|
Re: Virtual functions versus callbacks [message #24204 is a reply to message #24202] |
Tue, 05 January 2010 10:36 |
|
koldo wrote on Tue, 05 January 2010 03:59 | Hello all
To let the program do actions after events I have seen there are two options in Upp:
- To use "virtual" functions
For example to get the "enter" key in an EditField it is necessary to do a derived class
that catches virtual bool Key(dword key, int rep);
- To use callback
For example to do something after clicking in an ArrayCtrl it could be done by assigning a callback function to "WhenLeftClick".
From the user (programmer) point of view it seems better callback functions as you do not need to do derived classes every time you need a certain response after a Ctrl event.
Personally I stronger prefer callback but, what do you think about it ?
Best regards
Koldo
|
In my upp sources EditField has WhenEnter callback. I guess it could be added to the main tree as now I can see I'm not the only one who needs it
|
|
|
|
|
Re: Virtual functions versus callbacks [message #24219 is a reply to message #24218] |
Wed, 06 January 2010 11:29 |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
IMO the difference is that Callbacks are intended for communication between classes/Ctrls and are defined by the Ctrl. Callbacks are usually for actions that imply some sort of state change or contextual (wrong word maybe?) function of the Ctrl.
The virtual methods are the way in which a Ctrl interfaces internally with the underlying GUI/OS and is essentially a cleaner replacement of the message loop. The functions sometimes pass additional information that wouldn't be useful or needed externally.
Example of Callbacks:
WhenAction
WhenAcceptEdit
Example of virtual function:
LeftDown
GotFocus
While there are occasions when the developer might requrie some additional comunication from a Ctrl (ie a Callback) that is the exception rather than the rule in my experience and as demonstrated it is not difficult to add.
I think the current way works very well. Adding Callbacks for all events would just expose a lot of internal message stuff that wouldn't be used very often at the expense of making derived Ctrls more difficult.
IMO obviously, only Mirek could give a definitive answer.
[Updated on: Wed, 06 January 2010 11:38] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 21:36:11 CEST 2024
Total time taken to generate the page: 0.01844 seconds
|
|
|