How to force overloaded call in derived classes?

Discussion in 'C++' started by Marcin Gil, Jul 4, 2007.

  1. Marcin Gil

    Marcin Gil Guest

    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
     
    Marcin Gil, Jul 4, 2007
    #1
    1. Advertising

  2. Marcin Gil

    Ron Natalie Guest

    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);
    }
     
    Ron Natalie, Jul 4, 2007
    #2
    1. Advertising

  3. Marcin Gil

    Marcin Gil Guest

    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/
     
    Marcin Gil, Jul 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lagarde s?bastien

    Call of overloaded derived method problem

    Lagarde s?bastien, Jan 28, 2004, in forum: C++
    Replies:
    2
    Views:
    457
    Sreenivas M
    Jan 29, 2004
  2. A. W. Dunstan

    Force loading of derived classes?

    A. W. Dunstan, Jul 26, 2006, in forum: Java
    Replies:
    7
    Views:
    3,187
    Patricia Shanahan
    Jul 27, 2006
  3. Replies:
    1
    Views:
    419
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    405
    Victor Bazarov
    May 23, 2007
  5. Fokko Beekhof
    Replies:
    5
    Views:
    447
    Kai-Uwe Bux
    Aug 29, 2008
Loading...

Share This Page