Avoid dynamic_cast with derived classes (Cast Derived class)

前端 未结 3 1998
栀梦
栀梦 2021-01-06 23:43

I am new to C++ and came to a point, where I generate an overhead with classes. I have a QTcpSocket and read messages from it and create objects, for example MessageJoin, Me

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-07 00:41

    The visitor pattern could be a good fit i.e.

    class Message
    {
    public:
        QString virtual getRawMessage() { return dataRawMessage; }
    
        virtual void accept(Client& visitor) = 0;
    
    protected:
        QString dataRawMessage;
    };
    
    // Join class - cointains the name of the joined user and the channel
    class MessageJoin : public Message
    {
    public:
        MessageJoin(const QString &rawmessage, const QString &channel, const QString &user)
        {
            dataRawMessage = rawmessage;
            dataChannel = channel;
            dataUser = user;
        }
    
        QString getChannel() { return dataChannel; }
        QString getUser(){ return dataUser; }
    
        void accept(Client& visitor) override
        {
              visitor.visit(*this);
        }
    
    private:
        QString dataChannel;
        QString dataUser;
    };
    
    // Notice class - contains a notification message
    class MessageNotice : public Message
    {
    public:
        MessageNotice(const QString &rawmessage, const QString &text)
        {
            dataRawMessage = rawmessage;
            dataText = text;
        }
    
        QString getText() { return dataText;}
    
        void accept(Client& visitor) override
        {
              visitor.visit(*this);
        }
    
    private:
        QString dataText;
    };
    
    void Client::visit(MessageJoin& msg)
    {
        qDebug() << msg.getUser() << " joined " << msg.getChannel();
        // Update UI: Add user
    }
    
    void Client::visit(MessageNotice& msg)
    {
        qDebug() << msg.getText();
        // Update UI: Display message
    }
    
    // Client code - print message and update UI
    void Client::messageReceived(Message *message)
    {
        if(message)
        {
            message->visit(this);
            delete message; // Message was allocated in the library and is not used anymore
        }
    }
    

提交回复
热议问题