In C ++, how can I list an abstract class?

I have two classes implemented:

class DCCmd :
    public DCMessage

class DCReply :
    public DCMessage

Both are protocol messages that are sent and received in both directions.

Now in the implementation of the protocol I will need to create a message queue, but if DCMessageit is abstract, it will not allow me to do something like this:

class DCMsgQueue{
private:
    vector<DCMessage> queue;
public:
    DCMsgQueue(void);
    ~DCMsgQueue(void);

    bool isEmpty();
    void add(DCMessage &msg);
    bool deleteById(unsigned short seqNum);
    bool getById(unsigned short seqNum, DCMessage &msg);
};

The problem is that, as the compiler puts, โ€œDCMessage cannot be createdโ€ because it has a pure abstract method:

virtual BYTE *getParams()=0;

Removing =0and installing empty curly braces in DCMessage.cppfixes the problem, but it's just a hack.

Another solution is that I have to do two DCMsgQueues: DCCmdQueueand DCReplyQueue, but this is just duplicate code for something trivial. Any ideas? =)

+3
4

, , . , DCMessage, , , , .

vector<DCMessage*> queue;

DCCmd* commandObject = new DCCmd(...params...);
queue.push_back(commandObject);

BYTE* params = queue[0]->getParams();
+13

DCMessage:

vector<DCMessage*> messages;
messages.push_back(new DCCmd(blah));

++ , . , .

+10

( Kelix, , )

, , , , , ? vector <DCMessage>, DCMessage. , .

vector <DCMessage *>, , DCMessage, (runtime) .

+3

, ++, deque (, FIFO, LIFO). , .

++ - OO, ++ ; tcp , , . , , .

+2

Source: https://habr.com/ru/post/1717659/


All Articles