C++11 有没有办法将指针从一个队列移动到另一个队列?
我需要获取一个指向存储在一个队列中的对象的指针,处理它,然后移动到另一个对象中的另一个队列。接下来,我将展示问题的结构(示意图)C++11 有没有办法将指针从一个队列移动到另一个队列?,c++11,queue,polymorphism,C++11,Queue,Polymorphism,我需要获取一个指向存储在一个队列中的对象的指针,处理它,然后移动到另一个对象中的另一个队列。接下来,我将展示问题的结构(示意图) class A{ public: A(); ... // fields and methods } class B : public A{ public: B(); ... // extra fields and methods (not in A) } class C : public A{ p
class A{
public:
A();
...
// fields and methods
}
class B : public A{
public:
B();
...
// extra fields and methods (not in A)
}
class C : public A{
public:
C();
...
// extra fields and methods (not in A)
}
class Sender{
public:
Sender();
std::queue<A*> qSender;
...
// fields and methods
}
class Receiver{
public:
Receiver();
std::queue<A*> qReceiver;
void moveToReceiver(Sender& s, int n);
...
// fields and methods
}
但我注意到这些元素并不是流行的;代码仅“移动”n
指向qReceiver
的第一个对象的指针,而不是从qSender
中删除
欢迎任何解决这个问题的想法。这里是一个简化的示例。我使用了
std::unique_ptr
而不是原始的a*
,因为这有助于确保安全
#include <iostream>
#include <memory>
#include <queue>
#include <utility>
class A {
public:
A() = default;
// A virtual base class destructor is a must if you are going to destroy objects
// through a base class pointer:
virtual ~A() = default;
virtual void work() = 0; // an example function
};
class B : public A {
public:
void work() override { std::cout << "B working\n"; }
};
class C : public A {
public:
void work() override { std::cout << "C working\n"; }
};
// Helper alias
using queue_type = std::queue<std::unique_ptr<A>>;
void moveToReceiver(queue_type& sender, int n, queue_type& receiver) {
for(int i = 0; i < n; i++) {
// move the pointer out and put it in the new queue
receiver.emplace(std::move(sender.front()));
sender.pop();
}
}
// Display and empty a queue:
void display(queue_type& q) {
while(not q.empty()) {
q.front()->work();
q.pop();
}
}
int main() {
queue_type qSender;
queue_type qReceiver;
// Put two objects in qSender
qSender.emplace(std::make_unique<B>()); // C++11: new B
qSender.emplace(std::make_unique<C>()); // C++11: new C
// Move the objects to qReceiver
moveToReceiver(qSender, qSender.size(), qReceiver);
std::cout << "qSender:\n";
display(qSender);
std::cout << "qReceiver:\n";
display(qReceiver);
}
这里有一个简化的例子。我使用了
std::unique_ptr
而不是原始的a*
,因为这有助于确保安全
#include <iostream>
#include <memory>
#include <queue>
#include <utility>
class A {
public:
A() = default;
// A virtual base class destructor is a must if you are going to destroy objects
// through a base class pointer:
virtual ~A() = default;
virtual void work() = 0; // an example function
};
class B : public A {
public:
void work() override { std::cout << "B working\n"; }
};
class C : public A {
public:
void work() override { std::cout << "C working\n"; }
};
// Helper alias
using queue_type = std::queue<std::unique_ptr<A>>;
void moveToReceiver(queue_type& sender, int n, queue_type& receiver) {
for(int i = 0; i < n; i++) {
// move the pointer out and put it in the new queue
receiver.emplace(std::move(sender.front()));
sender.pop();
}
}
// Display and empty a queue:
void display(queue_type& q) {
while(not q.empty()) {
q.front()->work();
q.pop();
}
}
int main() {
queue_type qSender;
queue_type qReceiver;
// Put two objects in qSender
qSender.emplace(std::make_unique<B>()); // C++11: new B
qSender.emplace(std::make_unique<C>()); // C++11: new C
// Move the objects to qReceiver
moveToReceiver(qSender, qSender.size(), qReceiver);
std::cout << "qSender:\n";
display(qSender);
std::cout << "qReceiver:\n";
display(qReceiver);
}
是的,是std::queue。我现在进行编辑,为什么您认为ptr没有从qSender中删除?因为在“移动”之后,我打印了两个队列的内容,结果就是我所解释的。Qsender有相同的ptr,qReceiver有相同的ptr n时间,请您提供,以便我能看到您在做什么?输入错误;是(从另一个队列弹出的指针)是,是std::queue。我现在进行编辑,为什么您认为ptr没有从qSender中删除?因为在“移动”之后,我打印了两个队列的内容,结果就是我所解释的。Qsender有相同的ptr,qReceiver有相同的ptr n时间,请您提供,以便我能看到您在做什么?输入错误;是(从另一个队列弹出的指针)
qSender:
qReceiver:
B working
C working