event tables

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
mikecoon
Experienced Solver
Experienced Solver
Posts: 68
Joined: Wed May 04, 2005 7:24 pm

event tables

Post by mikecoon » Thu Aug 18, 2005 10:39 pm

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

User avatar
Ryan Norton
Moderator
Moderator
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: event tables

Post by Ryan Norton » Thu Aug 18, 2005 10:44 pm

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.
[Mostly retired moderator, still check in to clean up some stuff]

ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Re: event tables

Post by ssigala » Thu Aug 18, 2005 10:49 pm

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).
Sandro Sigala - Kynosoft, Brescia

mikecoon
Experienced Solver
Experienced Solver
Posts: 68
Joined: Wed May 04, 2005 7:24 pm

Post by mikecoon » Fri Aug 19, 2005 3:48 am

Thank you. Now I understand. I moved the enums and the event table to the .cpp file now.

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Fri Aug 19, 2005 7:16 am

Hi!
The enums can still reside in the header file. Use the enum inside your class.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

ssigala
Earned some good credits
Earned some good credits
Posts: 109
Joined: Fri Sep 03, 2004 9:30 am
Location: Brescia, Italy

Post by ssigala » Fri Aug 19, 2005 12:11 pm

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
    }
// ...
};
Sandro Sigala - Kynosoft, Brescia

bunter
In need of some credit
In need of some credit
Posts: 5
Joined: Wed Oct 22, 2008 5:16 am
Location: Russia

Handling events with a class in a header file only

Post by bunter » Mon Oct 26, 2009 8:12 am

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");
	}
};

...longer than the road that stretches out ahead...

Post Reply