c++memory-managementunique-ptrownership

Returning the raw pointer from a list of unique pointers


I have a class (A) that will own the memory for a set of instances of another class (B). A will store its instances of B as a set of std::unique_ptr<B>.

I want A to be able to return a reference to a set of raw pointers to those B objects, (i.e. I don't want any transfer of ownership). What is the best way to do this?

class A
{
private:
    std::set<std::unique_ptr<B>> _Bs;
public:
    std::set<B*>& getBs() { ???? };
}

I could create a new vector:

class A
{
private:
    std::set<std::unique_ptr<B>> _Bs;
    std::set<B*>                 _rawBs;
public:
    std::set<B*>& getBs() { return _rawBs; };
}
std::set<B*>
A::A()
{
    std::transform(_Bs.begin(), 
                   _Bs.end(), 
                   std::inserter(_rawBs, _rawBs.begin()), 
                   [](auto uniq_ptr) { uniq_ptr.get(); }
    
}

But it seems a bit janky to store these pointers twice...


Solution

  • You should add a member function:

    const std::set<std::unique_ptr<B>>& getBs() {
        return m_bs;
    }
    

    There are a few things to note: