Code:
#include <iostream>
#include <memory>
class Test {
public:
void TestFunc() {
std::cout << "TestFunc()\n";
}
};
int main() {
std::unique_ptr<Test> ptr1(new Test);
ptr1->TestFunc();
std::unique_ptr<Test> ptr2 = std::move(ptr1);
ptr2->TestFunc();
if (ptr1 == nullptr) {
std::cout << "Now ptr1 is nullptr\n";
}
ptr1->TestFunc(); // !!
}
Result:
TestFunc()
TestFunc()
Now ptr1 is nullptr
TestFunc() // !!
Like above code, the ownership of Test object is moved from ptr1 to ptr2.
However, still I can call TestFunc() through ptr1 even though ptr1 has confirmed as nullptr after moving. How is this possible?