开发者

event handling inheritance (C++)

I am redesigning how events (related to data) are handled for existing code (in C++). The events to be handled are DataTable related events, and XmlNode related events.

I'm wanting to create a general super class with virtual functions (calling it EventManager) in order to provide consistency across the application.

I plan to have the following virtual functions in the EventManager:

DataChanged(EventAr开发者_如何学Cgs arg)
DataChanging(EventArgs arg)
DataInserted(EventArgs arg)
DataInserting(EventArgs arg)
DataRemoved(EventArgs arg)
DataRemoving(EventArgs arg)

I have used EventArgs as a parameter because the others (XmlNodeChangeEventArgs, DataRowChangeEventArgs, DataColumnChangeEventArgs) are derived from it. But by using it in the virtual functions, I will have lost information unique to the derived event args.

My question, how should I be designing/handling this super class and the virtual functions in order to have a general class that manages events, but can still properly map (with all information needed) to the derived classes?

I did my best to explain my situation, but please feel free to ask questions to clarify the question. I'm really looking for well thought out answers, so I'm open to improving my question!

Thank you in advance for your time and patience! :)


  1. Pass the arguments by reference, passing by value just adds an additional overhead.

  2. But by using it in the virtual functions, I will have lost information unique to the derived event args.

You can use dynamic_cast and check the validity of the downcast, no information will be lost.


Apart from passing all your events by reference, instead of by value (which would slice them, so that you could not even cast them down), I would think of defining some more abstract way of looking at your events so that passing a reference to the base class would not hinder you from accessing all the functionality defined in derived classes.

I don't know if it is possible, but you could possibly make your EventArgs class a virtual base class exposing an interface generic enough to host all that you might need in derived classes.

If you specify more info on the operations and behavior you are envisaging for your events, I could try and provide more detailed suggestions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜