#include <iostream>
using namespace std;
class A{
private : static A *ob;
public : static A* fun1();
public : void show(string str){
cout<<"This is :- "<<str<<endl;
}
};
A* A::ob=NULL;
A* A::fun1(){
if(ob==NULL){
ob=new A();
return ob;
}
return NULL;
}
int main() {
A *ob1 = A::fun1();
A *ob2 = A::fun1();
ob1->show("A"); // line 1
ob2->show("B"); //line 2
return 0;
}
the output is :-
This is :- A
This is :- B
Although i expected only "This is :- A" since 'ob2' is NULL and dereferencing it should give error.
Can someone explain the output?