Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/bad weak ptr"

From cppreference.com
< cpp‎ | memory
m (Text replace - "{{cpp|" to "{{c|")
m (Text replace - "cpp/error/exception" to "cpp/error")
Line 16: Line 16:
 
{{dcl list end}}
 
{{dcl list end}}
  
{{cpp/error/exception/exception/inherit}}
+
{{cpp/error/exception/inherit}}
  
 
===Example===
 
===Example===

Revision as of 00:39, 29 April 2012

Template:cpp/memory/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <memory>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
class bad_weak_ptr : public std::exception;
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

std::bad_weak_ptr is the type of the object thrown as exceptions by the constructors of std::shared_ptr that take std::weak_ptr as the argument, when the std::weak_ptr refers to an already deleted object.

Contents

Member functions

Constructs the bad_weak_ptr object
(public member function)
Returns the explanatory string
(public member function)

Inherited from std::exception

Member functions

[virtual]
destroys the exception object
(virtual public member function of std::exception) [edit]
[virtual]
returns an explanatory string
(virtual public member function of std::exception) [edit]

Example

#include <memory>
#include <iostream>
int main()
{
    std::shared_ptr<int> p1(new int(42));
    std::weak_ptr<int> wp(p1);
    p1.reset();
    try {
        std::shared_ptr<int> p2(wp);
    } catch(const std::bad_weak_ptr& e) {
        std::cout << e.what() << '\n';
    }
}

Output:

std::bad_weak_ptr

See also

Template:cpp/memory/dcl list shared ptrTemplate:cpp/memory/dcl list weak ptr