parameters in this constructor is different from wxEvent VS wxCommandEvent

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
ollydbg23
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 224
Joined: Fri Dec 12, 2008 10:31 am

parameters in this constructor is different from wxEvent VS wxCommandEvent

Post by ollydbg23 » Wed Feb 24, 2021 2:46 pm

Today, I try to design a custom event, so I follow the wiki: https://wiki.wxwidgets.org/Custom_Events

Then I copy some code, and design my own.

Code: Select all

class MyFooEvent: public wxCommandEvent
{
public:
	MyFooEvent(wxEventType commandType = MY_NEW_TYPE, int id = 0)
        		:  wxCommandEvent(commandType, id) { }
 
	// You *must* copy here the data to be transported
	MyFooEvent(const MyFooEvent& event)
        		:  wxCommandEvent(event) { this->SetPoint(event.GetPoint()); }
 
	// Required for sending with wxPostEvent()
	wxEvent* Clone() const { return new MyFooEvent(*this); }
 
	wxRealPoint GetPoint() const { return m_RealPoint; }
	void SetPoint(const wxRealPoint& rp) { m_RealPoint = rp; }
 
private:
	wxRealPoint m_RealPoint;
};
Then I found that I don't need a wxCommandEvent, so I just replace the it with wxEvent. So, my code just becomes:

Code: Select all

...
MyFooEvent(wxEventType commandType = MY_NEW_TYPE, int id = 0)
        		:  wxEvent(commandType, id) { }
...        		
After that, I wrote the Connect/Bind function, and wxPostEvent, strange thing is the event handler never be called.

So, I try to check the error, and finally I found that in the wxEvent's document, there is one important note:

wxWidgets: wxEvent Class Reference
Also please notice that the order of parameters in this constructor is different from almost all the derived classes which specify the event type as the first argument.
So, I see that if I change the wxCommandEvent to wxEvent, the constructor's parameter should be swapped (id is the first parameter)

Code: Select all

...
MyFooEvent(wxEventType commandType = MY_NEW_TYPE, int id = 0)
        		:  wxEvent(id, commandType) { }
...        		
My question is: why it has such strange design? for wxEvent, id is the first parameter, and for other event type, wxEventType is the first parameter, those two parameters are just "int" type, so I don't see the compiler give some warning if I made a mistake.

Thanks.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2922
Joined: Sun Jan 03, 2010 5:45 pm

Re: parameters in this constructor is different from wxEvent VS wxCommandEvent

Post by PB » Wed Feb 24, 2021 2:51 pm

My guess would be the obvious: the different order allows you to specify the event type while not forcing you to list the values of other parameters.

ollydbg23
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 224
Joined: Fri Dec 12, 2008 10:31 am

Re: parameters in this constructor is different from wxEvent VS wxCommandEvent

Post by ollydbg23 » Thu Feb 25, 2021 1:09 am

PB wrote:
Wed Feb 24, 2021 2:51 pm
My guess would be the obvious: the different order allows you to specify the event type while not forcing you to list the values of other parameters.
OK, for wxCommandEvent like event, the most important parameter is event type.

Post Reply