473,670 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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 = &ap;

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
Jul 4 '07 #1
2 1911
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 CMessageProcess or (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 CMessageProcess or::ProcessGene ric(CMessage& m) {
CServerMessage* csm = dynamic_cast<CS erverMessage*)( &m);
if(csm) {
ProcessMessage( *csm);
return;
}
CTextMessage* ctm = dynamic_cast<CT extMessage*>(&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 CMessageProcess or (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 CMessageProcess or::ProcessGene ric(CMessage& m) {
CServerMessage* csm = dynamic_cast<CS erverMessage*)( &m);
if(csm) {
ProcessMessage( *csm);
return;
}
CTextMessage* ctm = dynamic_cast<CT extMessage*>(&m );
if(ctm) {
ProcessMessage( *ctm);
return;
}
ProcessMessage( m);
}
I did use Visitor pattern - this is how MessageType->Process(Proces sorType)
works.
However to enable full polymorphism/use overloading I would have to define
a ProcessMessage( MessageType) in CMessageProcess or for each message type
supported (as would uncommenting (1) in my previous post do).

I am also not sure if your "dynamic_ca st" 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
2880
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
3669
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 { /* ... */ }; template<class T> classSubject {
1
9999
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 A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
3
1589
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 app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
9
1138
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
3499
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 can be used by static member functions of the base class? Something like virtual static XClass* pXClass; /* pXClass shall be pure virtual, so not static in base class, but
1
1487
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
2021
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 overridden. Otherwise, the overloads of the overridden function in the base class will not be visible from the derived class." In other words...
5
3189
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6216
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.