Using nullptr to call function

时间:2016-02-20 19:48:38

标签: c++ nullptr

Sorry I couldn't think of any better title after I encountered the following piece of code while experimenting.

What I don't understand here is how could I call a function of the class using a nullptr in this case.

#include <memory>
#include <iostream>
using namespace std;

class test {
   public:
      void testfunc() {
         cout << "calling test func" << endl;
      }
};

int main () {
   unique_ptr<test> ptr;
   if (ptr == nullptr) {
      cout << "ptr is null" << endl;
   }

   ptr->testfunc();
   return 0;
}

According to me this should just crash because its a nullptr, but it doesn't and work like as if ptr is not a nullptr.

0 个答案:

没有答案