我想创建一个通知中心,我在其中处理到线程
的所有通知
。
我无法在软件启动时告诉我需要多少个notification
队列。它可能在运行时
期间发生变化。
所以我创建了这个(代码简化):
#include <vector>
#include "Poco/Notification.h"
#include "Poco/NotificationQueue.h"
using Poco::Notification;
using Poco::NotificationQueue;
int main()
{
std::vector<NotificationQueue> notificationCenter;
NotificationQueue q1;
NotificationQueue q2;
notificationCenter.push_back(q1); //ERROR: error: use of deleted function ‘Poco::NotificationQueue::NotificationQueue(const Poco::NotificationQueue&)’
notificationCenter.push_back(q2);
return 0;
}
我收到错误:使用已删除函数‘Poco::NotificationQueue::NotificationQueue(const Poco::NotificationQueue&)’
我明白了。我无法复制或分配 NotificationQueue
。
问题:
有什么方法可以处理 NotificationQueue
的 vector 而无需静态创建它们?
最佳答案
接受 @arynaq
评论,指针 vector 将完成这项工作:
#include <memory>
#include <vector>
#include "Poco/Notification.h"
#include "Poco/NotificationQueue.h"
using Poco::Notification;
using Poco::NotificationQueue;
int main()
{
std::vector<std::shared_ptr<NotificationQueue>> notificationCenter;
std::shared_ptr<NotificationQueue> q1 = std::make_shared<NotificationQueue>();
std::shared_ptr<NotificationQueue> q2 = std::make_shared<NotificationQueue>();
notificationCenter.push_back(q1);
notificationCenter.push_back(q2);
return 0;
}
关于C++ Poco - 如何创建 NotificationQueue 的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52280390/