我将智能指针的所有权从pa移到了pb。尽管pa的地址为NULL,但移动所有权后,该方法仍然有效。如果某个方法不涉及类中的任何成员变量,是否可能?
#include <iostream>
#include <memory>
class A {
int* data;
public:
A(){
data = new int[100];
std::cout << "Resources acquisition\n";
}
void some(){
std::cout << "Same as ordinary pointer " << "\n";
}
~A() {
std::cout << "Resource release\n";
delete[] data;
}
};
void do_something(){
std::unique_ptr<A> pa(new A());
// tranfer ownership from pa to pb
std::unique_ptr<A> pb = std::move(pa);
// address is 0x0
std::cout << pa.get() << std::endl;
// This is still working
pa->some();
}
int main() {
do_something();
return 0;
}