Messages Order

  • + 0 comments

    Here is my solution. I use a static counter for each message created and I attribute it a rank. These ranks are used in the operator< to retrieve the messages order.

    class Message {
    private:
        string text;
        static int nbMessages;
        int rank;    
    public: 
        Message() {}
        Message(const string& ptext): text(ptext) {
            nbMessages++;
            rank = nbMessages;    
        };    
        const string& get_text() {
            return text;
        }
        const int& get_rank() {
            return rank;
        }
        bool operator< (Message&);
    };
    bool Message::operator<(Message& msg){
        if(rank <= msg.get_rank()) return true;
        return false;
    }
    
    int Message::nbMessages = 0;
    
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            Message msg(text);
            return msg;
        }
    };