Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic Enum in C++

I have these Enum declarations:

enum MessageType{  
    REQ_LOGIN,
    REQ_GET_FIELD,       

    RES_LOGIN,
    RES_GET_FIELD
}

enum Request{
    REQ_LOGIN,
    REQ_GET_FIELD
};

enum Respond{
    RES_LOGIN,
    RES_GET_FIELD
};

Obviously I'm repeating elements in Enum's. Is there any way to prevent this?

EDIT: I'm using "MessageType" on a general purpose class to send it through network, on the other side I parse the object of this class and dispatch message. But I have different clients; some expects only objects with "Request" type member and some expects only objects with "Response" type member.

Using "Message" class, I'm creating "DispatcherRequest"s.

class Message
{
public:
……….
    MessageType messageType;
}


struct DispatcherRequest
{
..........
    Request type;
};
like image 823
metdos Avatar asked Jun 25 '10 11:06

metdos


1 Answers

Why not try something like this?

enum MainType{  
    REQUEST,
    RESPONSE
};

enum SubType{
    LOGIN,
    GET_FIELD
};

class Message {
   MainType type;
   SubType sub_type;
   ...
};
like image 121
PeterK Avatar answered Sep 24 '22 16:09

PeterK