I'm trying to use the Prototype Design Pattern with a queue to store values from it. My code compiles but does not run. I thought maybe my code was too inefficiently running, but it just stops instead of running slowly.
#include <iostream>
#include <vector>
#include<string>
#include<queue>
using namespace std;
class process
{
public:
process(const string state): state(state){}
virtual process* clone() =0;
virtual void report() =0;
virtual void changeState(const string&) =0;
virtual ~process(){}
protected:
string state="new";
};
class process1: public process{
public:
process1(const string state): process(state){}
// clone
process1* clone() override {return new process1(state);}
void report(){cout<<state;}
void changeState(const string& c){
if (c=="new")
state="Ready";
else if (c=="Ready")
state="Running";
else if (state=="Running")
{
int x=rand()%3;
if(x==0)state="Terminate";
if (x==1)state="Ready";
if (x==2)state="Waiting";
}
else if (c=="Waiting")
state="Ready";
}
};
int main()
{
queue<process*> q;
process1 a("new");
for(int i=0;i<4;++i)
q.push(a.clone());
}
Any suggestions on what to do would be appreciated.
Aucun commentaire:
Enregistrer un commentaire