473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How to force overloaded call in derived classes?

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 CMessageInterface;
struct CMessage;

class CMessageProcessor
{
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 CAnotherProcessor: public virtual CMessageProcessor
{
public:
virtual void ProcessMessage(CTextMessage& a) { std::cout << "Processing
CTextMessage in Another" << std::endl; };
};

class CDerivedProcessor: public CAnotherProcessor
{
public:
virtual void ProcessMessage(CTextMessage& a) { std::cout << "Processing
CTextMessage in Derived" << std::endl; };
};

struct CMessage
{
virtual void Process(CMessageProcessor& p) = 0;
virtual ~CMessage() {};
};

template<typename Self>
struct CMessageBase: public CMessage
{
virtual void Process(CMessageProcessor& p) {
p.ProcessMessage(*static_cast<Self*>(this)); }; // (2)
};

struct CMessage: public CMessageBase<CMessage>
{
};

struct CTextMessage: public CMessageBase<CTextMessage>
{
};

struct CServerMessage: public CMessageBase<CServerMessage>
{
};
int main()
{
CDerivedProcessor p;
CAnotherProcessor ap;
CMessageProcessor* pP = &p;
CMessageProcessor* pAP = &ap;

std::vector<CMessage*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(*pP); // (3)
rMsg.Process(*pAP);
//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 CAnotherProcessor and
CDerivedProcessor is called only when appropriate ProcessMessage is
defined in CMessageProcessor.

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
CMessageProcessor 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
CDerivedProcessor::ProcessMessage(CTextMessage) is called
instead of CMessageProcessor::ProcessMessage(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(CCustomMessageType) and calling Process() on
CCustomMessageType it will call appropriate ProcessMessage regardles
of what is defined in CMessageProcessor.

So: extending a CCustomProcessor to process new message types
should require only defining a new ProcessMessage(CCustomMessageType)
- at least this is what I would like to have.

Thank you
-Marcin
Jul 4 '07 #1
2 1900
To use dynamic typing, you'll need to use some RTTI feature.
Overloading isn't going to cut it.

One way is to build some sort of bridge between the CMessage
class heirarchy and the CMessageProcessor (take a look at the
Visitor design pattern).

Another, slightly kludgy but perhaps faster way would be to
do (if I'm understanding you properly:

void CMessageProcessor::ProcessGeneric(CMessage& m) {
CServerMessage* csm = dynamic_cast<CServerMessage*)(&m);
if(csm) {
ProcessMessage(*csm);
return;
}
CTextMessage* ctm = dynamic_cast<CTextMessage*>(&m);
if(ctm) {
ProcessMessage(*ctm);
return;
}
ProcessMessage(m);
}
Jul 4 '07 #2
Ron Natalie wrote:
To use dynamic typing, you'll need to use some RTTI feature.
Overloading isn't going to cut it.

One way is to build some sort of bridge between the CMessage
class heirarchy and the CMessageProcessor (take a look at the
Visitor design pattern).

Another, slightly kludgy but perhaps faster way would be to
do (if I'm understanding you properly:

void CMessageProcessor::ProcessGeneric(CMessage& m) {
CServerMessage* csm = dynamic_cast<CServerMessage*)(&m);
if(csm) {
ProcessMessage(*csm);
return;
}
CTextMessage* ctm = dynamic_cast<CTextMessage*>(&m);
if(ctm) {
ProcessMessage(*ctm);
return;
}
ProcessMessage(m);
}
I did use Visitor pattern - this is how MessageType->Process(ProcessorType)
works.
However to enable full polymorphism/use overloading I would have to define
a ProcessMessage(MessageType) in CMessageProcessor for each message type
supported (as would uncommenting (1) in my previous post do).

I am also not sure if your "dynamic_cast" solution is better - one has
to add an "if" each time a new type is introduced.

Thank you
--
Marcin
http://marcin.gilowie.pl/
http://picasaweb.google.com/Dentharg/
Jul 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
9
by: Tony Maresca | last post by:
My class: class foo { protected virtual void MyMember() { } } When this class is a base with any number of derivatives,
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
1
by: joseph cook | last post by:
I'm confused by this output. I would expect the more specific overloaded function to be selected: #include <iostream> class Base { }; class Derived : public Base {};
3
by: Rick | last post by:
One of the rules I have recommended for our new coding standard is as follows: "When overriding a base class virtual function, all overloads of that base class virtual function should also be...
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.