简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM