#Guidance for using refs / raw pointers / smart pointers

31 messages · Page 1 of 1 (latest)

drifting skiff
#

I know it applies differently in different situations, but I want to ask about the general recommended style.
I am currently getting a pointer from outside and store it inside of the class like this.

class Foo
{
private:
    T* m_P;
public:
    Foo(T* p) : m_P(p)
    {
    }
    ~Foo()
    {
        delete m_P;
    }
};

int main()
{
    T* a = someValue;
    Foo foo(a);
}

What I want is m_P and a pointing the same value.
This code works as I want, but using raw pointers feels unsecured.
What is the secured code of this program?

proven crestBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

snow solar
#

you rarely wanna use new or delete. a safe solution is using a std::unique_ptr, and then using std::unique_ptr::get to obtain raw pointers to the value to access it in multiple places. Make sure the std::unique_ptr outlives all the raw pointers obtained through std::unique_ptr::get.

drifting skiff
#

Then the safe way would be like this?

class Foo
{
private:
    T* m_P;
public:
    Foo(T* p) : m_P(p)
    {
    }
    ~Foo()
    {
        delete m_P;
    }
};

int main()
{
    unique_ptr<T> a = make_unique<T>(someValue); // make sure "a" outlives "m_P"
    Foo foo(a.get());
}
snow solar
#

yes (of course, you have to remove the delete in the destructor)

drifting skiff
#

Oh right

#

Can shared_ptr can be a solution too?

snow solar
#

in most cases, no

drifting skiff
#

I see

#

Thanks!

proven crestBOT
#

@drifting skiff Has your question been resolved? If so, run !solved :)

drifting skiff
#

!sovled

#

!solved

proven crestBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

tawdry marlin
#

what you are writing here is closely resembling the internal implementation of std::unique_ptr

#

the difference is that unique_ptr has the ability to customize the deleter used, they avuse operator() to customize this

#

this gives users the ability to pass a function pointer or a lambda or a functor to use custom defined cleanup code

#

unique_ptr also supports rule of 5, something which you do not have yet

drifting skiff
#

What if I want a exist?

#

Creating by new means there is no a in main function, right?

#

I want another variable, in here a, exist outside of the class, pointing the same value.

tawdry marlin
drifting skiff
drifting skiff
#

I want a exist. I understood that your style doesn't have a

tawdry marlin
#

a.k.a. segfault

#

or even worse

#

when you call the constructor of Foo, Foo has no idea who created the pointer, Foo assumes that it must call delete

drifting skiff
#

Well..Then looks like I should make T a instead of unique_ptr<T> a