我正在尝试写一个消息处理东西。主要组件是接收器,处理器,过滤器,翻译器和发送器。我想在处理器组件的“进程”功能中遇到单点故障,用于捕获异常,日志记录和指标。
正下方是控制台输出,显示出我的错误。
BASE
A
B
A
---FILTER---
FILTERED BASE
DID NOT FILTER A
Looks like a B <------THIS IS WHAT I WANT
DID NOT FILTER B
---PROCESS---
FILTERED BASE
DID NOT FILTER A
DID NOT FILTER B <-----THIS MAKES ME SAD
这些是类定义
#include <string>
#include <iostream>
#include <memory>
//**************MESSAGES ************************
class Message
{
public:
virtual std::string getOut() const {return std::string("BASE");};
};
class MsgA : public Message
{
public:
std::string getOut() const {return std::string("A");};
};
class MsgB : public Message
{
public:
std::string getOut() const {return std::string("B");};
};
//**************FILTERS ************************
class MsgFilter
{
public:
void filter(const std::tr1::shared_ptr<Message> msg)
{
if( msg->getOut().compare("BASE") == 0 )
{
std::cout << "FILTERED BASE" << std::endl;
}
else
{
std::cout << "DID NOT FILTER " << msg->getOut() << std::endl;
}
}
void filter(const std::tr1::shared_ptr<MsgB> msg)
{
std::cout << "Looks like a B" << std::endl;
filter((std::tr1::shared_ptr<Message>)msg);
}
};
//**************PROCESSORS ************************
class MsgProcessor
{
public:
MsgProcessor():myFilt(MsgFilter()){}
MsgFilter myFilt;
virtual void process(std::tr1::shared_ptr<Message> msg)
{
myFilt.filter(msg);
}
};
以下是主要功能
#include "AllClassDefs.h"
int main(int argc, char *argv[])
{
std::tr1::shared_ptr<Message> msg(new Message());
std::tr1::shared_ptr<MsgA> msgA(new MsgA());
std::tr1::shared_ptr<MsgB> msgB(new MsgB());
std::cout << msg->getOut() << std::endl;
std::cout << msgA->getOut() << std::endl;
std::cout << msgB->getOut() << std::endl;
std::tr1::shared_ptr<Message> msgAPtr(msgA);
std::cout << msgAPtr->getOut() << std::endl;
std::cout <<"\n\nFILTER---\n";
std::tr1::shared_ptr<MsgFilter> flt(new MsgFilter());
flt->filter(msg);
flt->filter( (std::tr1::shared_ptr<Message>) msgA);
flt->filter(msgB);
std::cout <<"\n\nPROCESS---\n";
std::tr1::shared_ptr<MsgProcessor> prc(new MsgProcessor());
prc->process(msg);
prc->process(msgA);
prc->process(msgB);
}
所以...我希望进程函数调用Filter(MsgB)函数而不是Filter(Message)函数。通过使用进程(消息)函数,它看起来像Filter将MsgB视为消息。我怎样才能让它很好地工作? 我知道我可以使用模板,但这似乎有点过分了......再加上一个有几百个消息类的系统,它可能会导致一个大的exe。我应该使用双重发送吗?或者是否有更简单的解决方案?
答案 0 :(得分:1)
你的问题是在编译时执行了重载解析,所以这个函数
virtual void process(std::tr1::shared_ptr<Message> msg)
将始终调用相同的重载
void filter(const std::tr1::shared_ptr<Message> msg)
因为它匹配参数的静态类型。
msg
有时恰好指向派生类并不重要。