Page 1 of 1

event tables

Posted: Thu Aug 18, 2005 10:39 pm
by mikecoon
According to the new wxWidget book, you should place the event table in the implementation file. Why is that? Because I all this time I always always placed the event table right after the class declaration in the header file and it always worked for me.

Is there a special reason to place your event table int he .cpp file?
mikecoon

Re: event tables

Posted: Thu Aug 18, 2005 10:44 pm
by Ryan Norton
mikecoon wrote:Is there a special reason to place your event table int he .cpp file?
mikecoon
Yeah - the same reason you don't implement anything in a header - in complex projects you're gonna get linker errors.

Even with a header gaurd if you include the header in more than one cpp file you'll get a linker error doing so.

Re: event tables

Posted: Thu Aug 18, 2005 10:49 pm
by ssigala
mikecoon wrote:Is there a special reason to place your event table int he .cpp file?
mikecoon
Because you should think the event table as a variable. Variables are never declared in headers files, because doing so will cause a linker error (multiple definitions).

Posted: Fri Aug 19, 2005 3:48 am
by mikecoon
Thank you. Now I understand. I moved the enums and the event table to the .cpp file now.

Posted: Fri Aug 19, 2005 7:16 am
by upCASE
Hi!
The enums can still reside in the header file. Use the enum inside your class.

Posted: Fri Aug 19, 2005 12:11 pm
by ssigala
upCASE wrote:Hi!
The enums can still reside in the header file. Use the enum inside your class.
Good suggestion upCASE. Including the enums into the class definition also keeps the C++ namespace clean.

e.g.:

Code: Select all

class MyFrame: public wxFrame {
// ...
public:
    // IDs of children objects 
    enum {
        ID_BUTTON1,
        ID_MENU_ITEM1
    }
// ...
};

Handling events with a class in a header file only

Posted: Mon Oct 26, 2009 8:12 am
by bunter
Also, there is a way to create only header file for a class and to make that class handle events.

This way event table macros are not used at all, and cpp file is not needed. Instead, dynamic event handling is used.

Code: Select all


class InHeaderEvtHandler : public wxEvtHandler
{
	wxTimer timer_;

	void Init()
	{
		timer_.SetOwner(this);
		timer_.Start(3000);
		Connect(wxTimerEvent().GetEventType(), wxTimerEventHandler(InHeaderEvtHandler::OnTimer));
	}
public:
	InHeaderEvtHandler() { Init(); }
	
	void OnTimer(wxTimerEvent& event)
	{
		wxMessageBox("InHeaderEvtHandler::OnTimer");
	}
};