[英]Command design pattern issue in c++
I implemented command design pattern this way , but if i uncomment destructor in command class , it is giving a linking error . 我以这种方式实现了命令设计模式,但是如果我在命令类中取消注释析构函数,则会出现链接错误。 Why ? 为什么呢
#include <iostream>
using namespace std;
//command design pattern
class command
{
public:
virtual void execute() = 0;
//virtual ~command();
};
class Receiver
{
public :
void action()
{
cout<<" command executed "<<endl;
}
};
class concreteCommand:public command
{
Receiver *_r;
public:
concreteCommand(Receiver *r = 0) :_r(r)
{}
void setReceiver(Receiver *r = 0)
{
_r = r;
}
virtual void execute()
{
if(0!=_r)
{
_r->action();
}
}
};
class invoker
{
command *_c;
public:
invoker(command* c):_c(c)
{}
void setCommand(command *c=0)
{
_c=c;
}
void invoke()
{
if(0!=_c)
{
_c->execute();
}
}
};
int main()
{
Receiver r;
concreteCommand c(&r);
invoker i(&c);
i.invoke();
return 0;
}
Found the error. 发现错误。 I have to provide body for destructor. 我必须为破坏者提供尸体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.