Does this unique_ptr own an object or a pointer to an object?

Viewed 53

I have code similar to the following:

CCell* pGroupCell = new CCell(
    this, RowNumber, s_NameColIndex, RowNumber,
    s_LastColIndex, NULL, RowState.GetName(), RowBehavior::Expanded, nullptr /* top-level group: no parent*/,
    _T(""), CCell::AlignLeft);

// Wrap the cells pointer in unique_ptrs for use by AddCellToGrid().  
std::unique_ptr<CCell>apGroupCell(pGroupCell);

if (!apGroupCell)
{
    return E_OUTOFMEMORY;
}

if (FAILED(AddCellToGrid(apGroupCell)))
    return E_FAIL;

The intent of this method is to pass the unique_ptr to the AddCellToGrid() method, which takes ownership of it. After the call to AddCellToGrid(), the unique_ptr is empty. Supposedly, the unique_ptr would delete the object when ownership is released.

But I'm not sure if that's what's really happening. The CCell object is being created on the heap, with a pointer to it being used to create the unique_ptr. But the unique_ptr is templatized on CCell, not on CCell*. So, I'm not sure if the unique_ptr really owns the object, or if it merely owns a pointer to the object.

0 Answers
Related