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
event tables
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: event tables
Yeah - the same reason you don't implement anything in a header - in complex projects you're gonna get linker errors.mikecoon wrote:Is there a special reason to place your event table int he .cpp file?
mikecoon
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]
-
- Earned some good credits
- Posts: 109
- Joined: Fri Sep 03, 2004 9:30 am
- Location: Brescia, Italy
Re: event tables
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).mikecoon wrote:Is there a special reason to place your event table int he .cpp file?
mikecoon
Sandro Sigala - Kynosoft, Brescia
-
- Earned some good credits
- Posts: 109
- Joined: Fri Sep 03, 2004 9:30 am
- Location: Brescia, Italy
Good suggestion upCASE. Including the enums into the class definition also keeps the C++ namespace clean.upCASE wrote:Hi!
The enums can still reside in the header file. Use the enum inside your class.
e.g.:
Code: Select all
class MyFrame: public wxFrame {
// ...
public:
// IDs of children objects
enum {
ID_BUTTON1,
ID_MENU_ITEM1
}
// ...
};
Sandro Sigala - Kynosoft, Brescia
Handling events with a class in a header file only
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.
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...