First of all thanks for your patience and good pointers
for Visitor and Curiously Recurring Template patterns last time :)
Now another problem of mine.
THE CODE
#include <iostream>
#include <vector>
struct CMessage;
struct CTextMessage;
struct CServerMessage;
struct CMessageInterfa ce;
struct CMessage;
class CMessageProcess or
{
public:
virtual void ProcessMessage( CMessage& m) { std::cout << "Processing any
message" << std::endl; };
// virtual void ProcessMessage( CTextMessage& a) { std::cout <<
"Processing CTextMessage" << std::endl; }; // (1)
// virtual void ProcessMessage( CServerMessage& c) { std::cout <<
"Processing CServerMessage" << std::endl; };
};
class CAnotherProcess or: public virtual CMessageProcess or
{
public:
virtual void ProcessMessage( CTextMessage& a) { std::cout << "Processing
CTextMessage in Another" << std::endl; };
};
class CDerivedProcess or: public CAnotherProcess or
{
public:
virtual void ProcessMessage( CTextMessage& a) { std::cout << "Processing
CTextMessage in Derived" << std::endl; };
};
struct CMessage
{
virtual void Process(CMessag eProcessor& p) = 0;
virtual ~CMessage() {};
};
template<typena me Self>
struct CMessageBase: public CMessage
{
virtual void Process(CMessag eProcessor& p) {
p.ProcessMessag e(*static_cast< Self*>(this)); }; // (2)
};
struct CMessage: public CMessageBase<CM essage>
{
};
struct CTextMessage: public CMessageBase<CT extMessage>
{
};
struct CServerMessage: public CMessageBase<CS erverMessage>
{
};
int main()
{
CDerivedProcess or p;
CAnotherProcess or ap;
CMessageProcess or* pP = &p;
CMessageProcess or* pAP = ≈
std::vector<CMe ssage*v;
v.push_back(new CMessage());
v.push_back(new CTextMessage()) ;
v.push_back(new CServerMessage( ));
for (int i = 0; i < 3; ++i)
{
CMessage& rMsg = *v.at(i);
rMsg.Process(*p P); // (3)
rMsg.Process(*p AP);
//v.at(i)->Process(*pP) ;
//v.at(i)->Process(*pAP );
}
while (!v.empty())
{
delete v.back();
v.pop_back();
}
return 0;
};
THE PROBLEM
An overloaded ProcessMessage( ) for CTextMessage in CAnotherProcess or and
CDerivedProcess or is called only when appropriate ProcessMessage is
defined in CMessageProcess or.
Seems obvious since at (3) the virtual function (1) of that definition
is unknown and virtuality doesn't play.
However I missed this since from the beginning I've used
CMessageProcess or with all these functions uncommented.
THE QUESTION
How can I make it that at (3) the call of (2) is done to the function I
want?
Ie: when rMsg at (3) is really CTextMessage and
CDerivedProcess or::ProcessMess age(CTextMessag e) is called
instead of CMessageProcess or::ProcessMess age(CMessage)?
THE NEED
I would like to have several custom message processors that are
able to process custom messages only by defining an overloaded
ProcessMessage( CCustomMessageT ype) and calling Process() on
CCustomMessageT ype it will call appropriate ProcessMessage regardles
of what is defined in CMessageProcess or.
So: extending a CCustomProcesso r to process new message types
should require only defining a new ProcessMessage( CCustomMessageT ype)
- at least this is what I would like to have.
Thank you
-Marcin