I have a problem when using an abstract class, with a derived base class, and a further derived subclass. The intention is that on creating an abstract pointer to the sub class, when an event fires and I call event.Skip(), the sub class and base class both handle the event. Without the abstract class with base class just inheriting wxEvtHandler, this works. As soon as I introduce the abstract class, which inherits wxEvtHandler, only sublcass sees the event twice.
I guess that makes sense, since the abstract class isn't instantiated, so I suspect inheriting with an abstract class is incorrect. So, how do I use an abstract class and derived classes that propagate events to the base class? I have posted the sample code of the problem below:
EventHeirarchyTest.cpp
Code: Select all
#include "EventHeirarchyTest.h"
#include "event.h"
#include "SubClass.h"
#include <wx/evtloop.h>
IMPLEMENT_APP_CONSOLE(EventHeirarchyTest)
enum EventList
{
SOCKET_ID
};
EventHeirarchyTest::EventHeirarchyTest()
{
}
EventHeirarchyTest::~EventHeirarchyTest(void)
{
}
bool EventHeirarchyTest::OnInit()
{
AbstractClass* serverC_ = new SubClass();
CustomEvent* event = new CustomEvent(CustomEventCommandEvent, SOCKET_ID);
event->SetEventObject(this);
wxQueueEvent(serverC_,event);
wxEventLoop loop;
loop.Run();
return true;
delete event;
delete serverC_;
}
int EventHeirarchyTest::OnRun()
{
return 0;
}
int EventHeirarchyTest::OnExit()
{
return 0;
}
Code: Select all
#pragma once
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//#include <iostream>
#ifdef __BORLANDC__
# pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
class EventHeirarchyTest : public wxAppConsole
{
public:
EventHeirarchyTest(void);
~EventHeirarchyTest(void);
virtual bool OnInit();
virtual int OnRun();
virtual int OnExit();
};
Code: Select all
#pragma once
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//#include <iostream>
#ifdef __BORLANDC__
# pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include "CustomEvent.h"
class AbstractClass
{
public:
AbstractClass(void);
~AbstractClass(void);
virtual void OnMyEvent(CustomEvent &event) =0;
};
Code: Select all
#include "BaseClass.h"
enum EventList
{
SOCKET_ID
};
BEGIN_EVENT_TABLE(BaseClass, AbstractClass)
EVT_DATA_AVAILABLE(SOCKET_ID, BaseClass::OnMyEvent)
END_EVENT_TABLE()
BaseClass::BaseClass(void)
{
}
BaseClass::~BaseClass(void)
{
}
void BaseClass::OnMyEvent(CustomEvent &event)
{
std::cout << "BaseClass::OnMyEvent\n";
}
Code: Select all
#pragma once
#include "CustomEvent.h"
#include "AbstractClass.h"
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//#include <iostream>
#ifdef __BORLANDC__
# pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
class BaseClass : public AbstractClass
{
public:
BaseClass(void);
~BaseClass(void);
void OnMyEvent(CustomEvent &event);
protected:
DECLARE_EVENT_TABLE()
};
Code: Select all
#include "SubClass.h"
enum EventList
{
SOCKET_ID
};
BEGIN_EVENT_TABLE(SubClass, BaseClass)
EVT_DATA_AVAILABLE(SOCKET_ID, SubClass::OnMyEvent)
END_EVENT_TABLE()
SubClass::SubClass(void)
{
}
SubClass::~SubClass(void)
{
}
void SubClass::OnMyEvent(CustomEvent &event)
{
std::cout << "SubClass::OnMyEvent\n";
event.Skip();
}
Code: Select all
#pragma once
#include "baseclass.h"
#include "CustomEvent.h"
#pragma once
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//#include <iostream>
#ifdef __BORLANDC__
# pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
class SubClass : public BaseClass
{
public:
SubClass(void);
~SubClass(void);
void OnMyEvent(CustomEvent &event);
protected:
DECLARE_EVENT_TABLE()
};