vendredi 31 août 2018

using unique_ptr in constructor

I am working on a school project and I cannot figure out unique_ptr usage.

class ClassName
{
private: 
    unique_ptr <bool[]> uniquePtr;

    void func(unique_ptr<bool[]>& refPtr) const
        {
            refPtr[0] = true;
            refPtr[1] = false; 
        }
public:
    //other things that will use the array uniquePtr
};

ClassName::ClassName()
{
    bool* boolName = new bool [someSize()];
    uniquePtr = unique_ptr<bool[]>(boolName);
    func(uniquePtr);
}

I understand that this does not work because uniquePtr is destroyed as func() finishes. I cannot figure out how to modify uniquePtr such that it will be accessible to my other functions. I do not have tried creating a new unique_ptr to pass into func() and then use move(uniquePtr) but that won't even compile.

Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire