0
class AA
{
public:
    void print(){std::cout<<"in class A"<<std::endl;}
};
int _tmain(int argc, _TCHAR* argv[])
{
    AA *a= NULL;
    a->print();
    return 0;
}

Above code outputs "in class A" . Since a is pointing to NULL, how print function can be called when object is pointing to NULL.

anand
  • 11,071
  • 28
  • 101
  • 159
  • 1
    This has been asked many times. In fact, the first related link on the right. It's undefined behaviour. What it does or doesn't do is of no concern to anyone. – chris Jul 12 '13 at 09:11
  • Hint : if you make your `print` method `virtual`, theeeen... – Nbr44 Jul 12 '13 at 09:12
  • someone recently asked me and my first answer was it should compile fine but it should give run time error NULL->print(); – anand Jul 12 '13 at 09:12
  • @Alien01, I don't know how well it's explained in the dupe, but it's **probably** because you don't use the object in the function. – chris Jul 12 '13 at 09:14
  • @Alien01 It should do _whatever_, because since the behaviour is undefined, it can do _anything_ without being wrong. – Daniel Daranas Jul 12 '13 at 09:14

0 Answers0