I got a queue (here for simplification: just a single variable) holding various kinds of messages.
InboxMessage inbox_queue_;
Multiple threads/classes can write messages into that queue. A consumer class reads them and processes them based on the kind of message that has been read.
class StatusMessage : public InboxMessage {
public:
std::string getStatus();
std::string getTimestamp();
// ...
};
class RandomMessage : public InboxMessage {
public:
std::string getCode();
int getCount();
// ...
};
The derived classes hold different kinds of attributes which must be accessed when handling the message.
My question is: is there any way to avoid downcasting by the consumer class in this scenario? Should it be avoided at all costs (if so, then how?).
I'm using dynamic_cast to make sure the program checks if the cast is valid and I can react to bad casts.
Thanks in advance!