1

I'm working on some kind of smart pointer technique but there is one piece I'm missing. I tried several combinations but the logic is as follow:

UInt *obj = new UInt;
UInt *ref;
ref = obj;

delete obj;
obj = NULL;

if (ref == NULL)
{
    // It works
}
else
{
    // It failed
}

Is there any way to hit "It Works" without setting ref to NULL explicitly?

EDIT:

A more appropriate scenario would be something like this:

class A
{
public:

    A(): ref(NULL) {}
    ~A()
    {
        if (ref != NULL)
            delete ref;
    }
    int *ref;
};

    int *obj = new int;
    A *host = new A();

    host->ref = obj; ???

    delete obj;
      obj = NULL;

    if (host->ref == NULL)
    {
        // It works.
    }
    else
    {
        // It failed.
    }

...

Can't use int*& ref as a class member though.... must be close.

6
  • 1
    Is UInt your smart pointer class? Smart pointers should not be allocated on the heap. Commented Feb 1, 2013 at 16:37
  • 1
    What does "pointer of pointer" stand for in the title of the question? Commented Feb 1, 2013 at 16:38
  • There are no smart pointers in this code. Commented Feb 1, 2013 at 16:39
  • I'm sorry, forget about the smart pointer scenario. UInt could be int instead. Commented Feb 1, 2013 at 17:06
  • 1
    yeah i am going to have to concur with the other comments. those pointers seem pretty dumb to me. Commented Feb 1, 2013 at 17:07

3 Answers 3

4

As you say, you should be using a smart pointer:

#include <memory>

std::shared_ptr<UInt> obj = std::make_shared<UInt>();
std::weak_ptr<UInt> ref = obj;

obj.reset();

if (ref.expired())
{
    // It works
}
else
{
    // It failed
}

Don't try managing your own memory when the standard library has facilities to do it for you.

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

2 Comments

Exactly, so that people who read your code will not need to read and understand your own implementation, when there is a standard that everybody knows. Also, most probably, standard will have it better than yours.
True, I never use the std lib, I guess I could give it a try. I'm still interested how I could accomplish the example.
3

Declare ref as a reference to pointer

Uint*& ref = obj;

ref will now refer to the obj pointer.

Comments

1

Intially both obj and ref point to the (same) UInt instance. Then you delete the instance and set obj to NULL. But ref is just like any other regular pointer, so it still points to the deleted instance.

Instead you can create a reference variable, or in this case a 'reference pointer' by adding & to the declaration:

Uint*& ref = obj;

Then ref really refers to obj and keeps the same value (pointer) as obj.

2 Comments

Oh excellent, I used this example for simplicity sake but what if ref is a class member?
stackoverflow.com/questions/892133/… provides some details, cons and pros of references as class members vs pointers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.