Skip to main content
added 182 characters in body
Source Link
Fantastic Mr Fox
  • 34.5k
  • 28
  • 105
  • 193

As stated in other answers, this is not possible with a raw pointer of the form SomeObject* somePointer. However, c++11 introduced a new set of dynamic memory management and new smart pointers. Using a smart pointer you can detect if the resource is still available. For example in the following:

std::weak_ptr<int> w; // Our pointer to a resource. { std::shared_pointer<int> s = std::make_shared<int>(5); // The resource. w = s; // We can set the weak pointer to the shared pointer. auto s2 = w; // Here we can promote the weak pointer to a shared pointer to control // the resource. *s2 = 6; // Here we can use the resource. } // Here the resource is destroyed. auto s2 = w; // Here we will fail to get the resource because it has been destroyed. We // have successfully used smart pointers to detect if the resource exists. 

Read more about std::shared_ptr and std::weak_ptr for more examples. Before c++11 equivelent types of smart pointers are available in boost.

As stated in other answers, this is not possible with a raw pointer of the form SomeObject* somePointer. However, c++11 introduced dynamic memory management and smart pointers. Using a smart pointer you can detect if the resource is still available. For example in the following:

std::weak_ptr<int> w; // Our pointer to a resource. { std::shared_pointer<int> s = std::make_shared<int>(5); // The resource. w = s; // We can set the weak pointer to the shared pointer. auto s2 = w; // Here we can promote the weak pointer to a shared pointer to control // the resource. *s2 = 6; // Here we can use the resource. } // Here the resource is destroyed. auto s2 = w; // Here we will fail to get the resource because it has been destroyed. We // have successfully used smart pointers to detect if the resource exists. 

Read more about std::shared_ptr and std::weak_ptr for more examples.

As stated in other answers, this is not possible with a raw pointer of the form SomeObject* somePointer. However, c++11 introduced a new set of dynamic memory management and new smart pointers. Using a smart pointer you can detect if the resource is still available. For example in the following:

std::weak_ptr<int> w; // Our pointer to a resource. { std::shared_pointer<int> s = std::make_shared<int>(5); // The resource. w = s; // We can set the weak pointer to the shared pointer. auto s2 = w; // Here we can promote the weak pointer to a shared pointer to control // the resource. *s2 = 6; // Here we can use the resource. } // Here the resource is destroyed. auto s2 = w; // Here we will fail to get the resource because it has been destroyed. We // have successfully used smart pointers to detect if the resource exists. 

Read more about std::shared_ptr and std::weak_ptr for more examples. Before c++11 equivelent types of smart pointers are available in boost.

Source Link
Fantastic Mr Fox
  • 34.5k
  • 28
  • 105
  • 193

As stated in other answers, this is not possible with a raw pointer of the form SomeObject* somePointer. However, c++11 introduced dynamic memory management and smart pointers. Using a smart pointer you can detect if the resource is still available. For example in the following:

std::weak_ptr<int> w; // Our pointer to a resource. { std::shared_pointer<int> s = std::make_shared<int>(5); // The resource. w = s; // We can set the weak pointer to the shared pointer. auto s2 = w; // Here we can promote the weak pointer to a shared pointer to control // the resource. *s2 = 6; // Here we can use the resource. } // Here the resource is destroyed. auto s2 = w; // Here we will fail to get the resource because it has been destroyed. We // have successfully used smart pointers to detect if the resource exists. 

Read more about std::shared_ptr and std::weak_ptr for more examples.