0

Why does this code work, when pointer is not pointed to any object? Output- It is calling show_base() without even pointing to any object.

#include<iostream>
using namespace std;
class base
{
    public:
        void show_base()
        {
            cout<<" It is a base class"<<endl;
        }

};
class derived: public base
{
    public:
        void show_derived()
        {
            cout<<" It is derived class"<<endl;
        }
};
int main()
{
    base *ptr=NULL;
    ptr->show_base();

}
  • What do you think should happen? Serious question. If you think it should crash, then I'm afraid the C++ standard doesn't say that. All it says is that your program has *undefined behaviour*. – john Apr 12 '20 at 14:04
  • It works [thanks to demons flying out of your nose](http://www.catb.org/jargon/html/N/nasal-demons.html). Your nasal demons can do many fascinating things, and this is just one of them. – Sam Varshavchik Apr 12 '20 at 14:09

0 Answers0