0

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);
    for(int i =0; i<someSize(); i++)
        {
            cout << uniquePtr[i];
            //This is all O's
        }
}

This does not work because uniquePtr is set to all 0s 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.

If the uniquePtr is modified by a function, in this case assigning it boolean 1 or 0, shouldnt it be accessible to function outside of that in the class? If I print uniquePtr[index] within the function it gives me the expected results.

Thanks for the help!

7
  • Possible duplicate of How can I initialize C++ object member variables in the constructor? Commented Aug 31, 2018 at 11:16
  • uniquePtr is destroyed as func() finishes No it is not, why would you think so? Commented Aug 31, 2018 at 11:17
  • @hellow OP should use bases-members-initialiser, but this is unrelated to the question being asked. Commented Aug 31, 2018 at 11:19
  • 1
    uniquePtr is a non-static member, so its lifetime is connected to the lifetime of the ClassName object. What specific trouble are you having that makes you conclude "this does not work"? Commented Aug 31, 2018 at 11:19
  • What is the purpose of func Why does it have to be non-static? Why does it have to be const qualified? Why do you need to pass the pointer to the function as an argument? Can't you simply use it like any other normal member variable? Commented Aug 31, 2018 at 11:25

1 Answer 1

1

A unique_ptr is not destroyed by passing it to a function by reference, so you are safe here.

However, passing unique_ptr by reference may not be the best way to express the idea. A non-owning pointer is represented by a plain C-style pointer in C++, so it's safe and idiomatic to do this:

void func(bool (*ptr)[]) const { ...

and then

func(uniquePtr.get());

As mentioned in the comments, data members should be initialised like this:

ClassName::ClassName() : unique_ptr(new bool[someSize()]) {
    func(uniquePtr.get());
}

(use std::make_unique instead of new if you can)

Sign up to request clarification or add additional context in comments.

5 Comments

I meant that uniquePtr is deleted when func() finishes. The reason I thought that is because whenever I try and access uniquePtr[index] it is all set to 0.
@jav_solo Please ask a question about, and show, what you have tried. If you tried to access uniquePtr[index] and it didn't work, code that does it should be in the question. Ask anothewr question and provide a minimal reproducible example.
I have updated the question to reflect my misunderstanding.
@jav_solo No, you should ask a NEW question and provide a minimal reproducible example. Please read carefully what this means.
Since what you have provided is not verifiable I'm voting to close this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.