我是智能指针的新手。不过,我对它有一个基本的了解。我观察到的是,智能指针必须以与其创建相反的顺序被销毁,否则智能指针可能会出现异常。考虑以下情况:
sharedPtr<abc> my_ptr(new abc); //smart pointer created. Instance counter = 1.
func1(my_ptr); //copy constructor in smart pointer called. Instance counter=2
func2(my_ptr); //copy constructor in smart pointer called. Instance counter=3
func3(my_ptr); //copy constructor in smart pointer called. Instance counter=4
现在,不是必须func3()先退出func2(),然后是func1()my_ptr。
问题:如果my_ptr首先超出范围(& 因此尝试删除abc),使用func1(),func2()并且func3()仍然引用abc(通过智能指针)怎么办?