1

I moved smart pointer ownership from pa to pb. A method still worked after moving ownership although the address of the pa is NULL. Is this possible if a method doesn't touch any member variable in a class?

#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;
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Kihwan Kim
  • 13
  • 4

0 Answers0