我正在尝试使用std::shared_ptr
and来处理对象std::weak_ptr
。场景是这样的:
我有从抽象类(具有纯虚函数)channel
派生的类对象。abstract::channel
我有一个容器channelContainer
( ),其中包含指向对象std::vector
的共享指针 ( std::shared_ptr
) 。channel
现在,我有一个包含deque (std::deque)
指向. 让我们命名这个双端队列。(std::weak_ptr)
channelContainer
freeChannelQueue
所以让我们说:
std::vector<std::shared_ptr<abstract::channel> > channelContainer;
std::deque<std::weak_ptr<abstract::channel > > freeChannelQueue;
//Assuming that both the containers are filled appropriately How do I go about implementeing the below functions?
abstract::channel& get_free_channel() {
//This should return a free channel object from 'freeChannelQueue' and pop the queue.
}
bool release_channel(abstract::channel& ch) {
//This should convert 'ch' to a std::weak_ptr (or std::shared_ptr) and push it to 'freeChannelQueue'
}
我对“如何将对对象的引用转换为弱指针”特别感兴趣?