简体   繁体   English

如何列出模板抽象类

[英]how to make a list of template abstract class

I must to do a list of template abstract base classes (and I have the delivered classes too) but I don't can inizialize the element of my list because the element is an abstract class... 我必须列出模板抽象基类的列表(我也有提供的类),但是我不能使列表中的元素具体化,因为该元素是抽象类...

this is my declaration: 这是我的声明:

/* fsm (list node) declaration */
template<class step_type> class fsm {
    protected:
        step_type   step;
        step_type   step_old;
        step_type   step_tmp;
        char        name[256];
        fsm *next;
        fsm *prev;
    public:
        fsm(step_type step);
        virtual void update() = 0;
        void show(){cout << step << ' ' << step_tmp << '\n'; };
        void init(step_type st_current) {step = st_current;};
//metodi per gestione nodo lista
        step_type getStep()     { return step; }
        fsm* getNext()          { return next; }
        fsm* getPrev()          { return prev; }

        void setStep(step_type s)   { step = s; }
        void setNext(fsm *n)        { next = n; }
        void setPrev(fsm *p)        { prev = p; }

};

/* fsm_List declaration */
template <class step_type>
class fsm_List
{
    fsm<step_type> *head, *tail;
    int size;

public:
    fsm_List();

    fsm<step_type>* getHead()    { return head; }
    fsm<step_type>* getTail()    { return tail; }
    int getSize()        { return size; }

    void insert(fsm<step_type> *n);    // add node to list
    void insert(step_type &value);        // new node and add in list
    fsm<step_type> *search(step_type &value);    //first node with value
    void delnode(fsm<step_type> *n);    // remove node
    int delvalue(step_type &value);        // remove all nodes

};

this is my delivered class: 这是我的授课内容:

class deri_pinza : public fsm<pin_steps>{
    private:
        bool    cmd_prelevamento_done;
    public:
        deri_pinza(): fsm<pin_steps>(ST_PIN_BOOT){
            cmd_prelevamento_done = false;
        };
        void update();
};

where: 哪里:

enum pin_steps {
    ST_PIN_BOOT,
    ST_PIN_CHECK_MOTORE,
    ST_PIN_ZERO_MOTORE,
    ST_PIN_WAIT_ZERO_MOTORE,
    ST_PIN_OPEN,
    ST_PIN_READY,
};

I have tryed to test in my main, but it's wrong... 我已经尝试过测试我的主要内容,但这是错误的...

    fsm<pin_steps> *one, *two, *three, *four, *five;

    one = new fsm<pin_steps>(ST_PIN_CHECK_MOTORE);
    two = new fsm<pin_steps>(ST_PIN_ZERO_MOTORE);
    three = new fsm<pin_steps>(ST_PIN_WAIT_ZERO_MOTORE);
    four = new fsm<pin_steps>(ST_PIN_OPEN);
    five = new fsm<pin_steps>(ST_PIN_READY);


    fsm_List<pin_steps> *mylist = new fsm_List<pin_steps>();

    (*mylist)+=(*one);
    (*mylist)+=(*two);


    mylist->insert(one);
    mylist->insert(two);


    cout << *mylist << endl;

how can I inizialize the List without inizialize fsm ( abstract class)? 如何在不初始化fsm(抽象类)的情况下初始化列表?

you can't create an instance of fsm<> with new , since it's abstract - it contains the pure virtual method virtual void update()=0; 您不能使用new创建fsm<>的实例,因为它是抽象的-它包含纯虚拟方法virtual void update()=0;

you can for example: 您可以例如:

fsm<pin_steps> *one...
one = new deri_pinza;

this is legal - and go on from here... 这是合法的-从这里继续...

EDIT - followup to our comments: 编辑-跟进我们的评论:

if you need a more general deri pinza (a generic one), it can be defined as: 如果您需要更通用的deri pinza( 通用 ),可以将其定义为:

template <typename STEP_TYPE>
class deri_pinza_gen : public fsm<STEP_TYPE> {
private:
    bool cmd_prelevamento_done;
public:
    deri_pinza_gen(STEP_TYPE step) : fsm<STEP_TYPE>(step){
        cmd_prelevamento_done = false;
    };
    virtual void update();
    virtual ~deri_pinza_gen();
};

and then: 接着:

mylist->insert( new deri_pinza_gen<pin_steps>(ST_PIN_BOOT) );
mylist->insert( new deri_pinza_gen<pin_steps>(ST_PIN_CHECK_MOTORE) );

ANOTHER_list->insert( new deri_pinza_gen<ANOTHER_pin_steps>(ANTHER_enum_item) );
...

are valid insertions. 是有效的插入。 I have declared update() virtual here, so you can derive your deri_pinza from this one, if you need it. 我在这里声明了update()为虚拟的,因此,如果需要,您可以从此派生deri_pinza。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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