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!

Jav Solo
  • 576
  • 1
  • 6
  • 15
  • Possible duplicate of [How can I initialize C++ object member variables in the constructor?](https://stackoverflow.com/questions/12927169/how-can-i-initialize-c-object-member-variables-in-the-constructor) – hellow Aug 31 '18 at 11:16
  • *uniquePtr is destroyed as func() finishes* No it is not, why would you think so? – n. m. could be an AI Aug 31 '18 at 11:17
  • @hellow OP should use bases-members-initialiser, but this is unrelated to the question being asked. – n. m. could be an AI Aug 31 '18 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"? – aschepler Aug 31 '18 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? – Some programmer dude Aug 31 '18 at 11:25
  • And no, `uniquePtr` ***isn't'*** "destroyed as func() finishes". That's because you pass the pointer *by reference* so the ownership isn't transferred when you call the function. – Some programmer dude Aug 31 '18 at 11:26
  • This is for a prime number algorithm function. As for why it has to be non-static, because my professor says so and we are not allowed to modify the header. – Jav Solo Aug 31 '18 at 13:16

1 Answers1

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)

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • 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 Aug 31 '18 at 13:14
  • @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 [mcve]. – n. m. could be an AI Aug 31 '18 at 14:41
  • I have updated the question to reflect my misunderstanding. – Jav Solo Aug 31 '18 at 14:50
  • @jav_solo No, you should ask a **NEW** question and provide a [mcve]. Please read carefully what this means. – n. m. could be an AI Aug 31 '18 at 15:27
  • Since what you have provided is [not verifiable](https://ideone.com/bzTxdM) I'm voting to close this question. – n. m. could be an AI Aug 31 '18 at 15:31