无法更新 class 内字段的值

Unable to update the value of field inside class

此函数从消息容器中删除过期消息

void Broker::removeExpiredMessages(){
    while(true){
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        messageMut.lock();
        std::cout<<Broker::getMessages().size()<<std::endl;
        for(auto& i : Broker::getMessages()){
            if(i.second.getHeader().expireAfter <= 0){
                std::cout<<"del"<<std::endl;
                Broker::getMessages().erase(i.first);
            }
           else
             i.second.getHeader().setExpireAfter(i.second.getHeader().getExpireAfter()-1);
        }
        messageMut.unlock();
    }
}

Message.hpp

class Message{
public:
    struct Header{
        std::string time;
        int expireAfter;
        std::string topicName;

        int getExpireAfter() const {
            return expireAfter;
        }

        void setExpireAfter(int expireAfter) {
            this->expireAfter = expireAfter;
        }

        const std::string& getTime() const {
            return time;
        }

        void setTime(const std::string &time) {
            this->time = time;
        }

        const std::string& getTopicName() const {
            return topicName;
        }

        void setTopicName(const std::string &topicName) {
            this->topicName = topicName;
        }
    };
    Message(){}
    void setPayload(std::string _payload){
        this->payload = _payload;
    }
    std::string getPayload()const{
        return this->payload;
    }

    void setHeader(const std::string& _time, const int _expireAfter, const std::string _topicName){
        this->header.setExpireAfter(_expireAfter);
        this->header.setTime(_time);
        this->header.setTopicName(_topicName);
    }

    Header getHeader()const{
        return this->header;
    }

private:
    Header header;
    std::string payload;
};

Broker.hpp

class Broker{
public:
    static bool isSubscriberLoopRunning;
    Broker(){std::cout<<"Broker()"<<std::endl;}
    static std::queue<const char*>& getMessageQueue() {
        return messageQueue;
    }

    static std::unordered_map<const char*, Message>& getMessages() {
        return messages;
    }

    static std::vector<Subscriber>& getSubscriberList() {
        return subscriberList;
    }

    static void pushNewSubscriber(Subscriber&);
    static void receiveMessageFromPublisher(const Message&);
    static void sendToSubscriber();
    static void removeSubscriber(const Subscriber&);
    static void removeExpiredMessages();
private:
    static std::unordered_map<const char*,Message> messages;
    static std::queue<const char*> messageQueue;
    static std::vector<Subscriber> subscriberList;
};

我正在尝试制作消息系统,我有一个 Broker 作为订阅者和发布者共享消息的中间件,消息存储在 unordered_set<const char*,Message> messages 中。Broker 的一个特点是它会自动删除过期的消息,为此,我在一个与其父线程分离的单独线程中创建了一个函数 运行。

我的问题是它没有删除到期时间已到的消息,因为即使我正在做 expireAfter 变量也没有递减 it.I 无法理解为什么它不更新值字段 expireAfter

你需要这个来return参考

Header &getHeader() const{
    return this->header;
}

它 return 现在按值而不是按引用。这就是过期时间不递减的原因!