std::shared_ptr
とstd::weak_ptr
を使用してオブジェクトをジャグリングしようとしています。このシナリオは次のようなものです:shared_ptr&weak_ptr conversions
私はクラスchannel
のオブジェクトを抽象クラスabstract::channel
(純粋な仮想関数を使用しています)から派生させています。私はコンテナchannelContainer
(std::vector
)に、共有ポインタ(std::shared_ptr
)〜channel
オブジェクトを含んでいます。
ここでは、のオブジェクトのそれぞれにウィークポインタ(std::weak_ptr)
を含むがあります。このdequeの名前を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'
}
は、私が特に興味を持っています
AFAIKオブジェクトまたはオブジェクト参照から 'weak_ptr'を直接初期化/構築することはできません。参照するオブジェクトは' shared_ptr'によって所有されている必要があります。したがって、構築のために 'shared_ptr'または' weak_ptr'が必要です。 – Piotr99