Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- {
- public:
- A(){}
- virtual void test(){std::cout << m;}
- int m;
- };
- int main()
- {
- A* a = new A();
- std::function<void ()> function = [=]()->void
- {
- //if( pointer to 'a' still valid )
- {
- a->test();
- }
- };
- delete a;
- //or if( pointer to 'a' still valid )
- function();
- system("pause");
- return 0;
- }
- std::shared_ptr<A> a(new A());
- std::weak_ptr<A> weak_a(a);
- std::function<void ()> function = [weak_a]()->void
- {
- if( std::shared_ptr<A> a = weak_a.lock() )
- {
- // to get the A* from a, do a.get()
- // operator-> and *a work however:
- a->test();
- }
- };
- a.reset(); // instead of delete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement