Nuthing is impossible !!Even a null pointer can access member functions
-
HI All ,:) I am a VC++ programmer,and some time My Acumen ask Microsofts VC++ (Visual Studio VC++ 6.0 )complier to complile my senseless programs ! :) See what I have asked this time to compile !!.. This is console based program, in which i am trying to call Member function of My class With a NULL pointer . And it is working fine....:cool: Please some one explain this to me ....is this happening of coz Compiler is fed up with my programs !! :-D #include "stdafx.h" #include class A { public : int var; A() { cout<<"In Consrtuctor !!"; } ~A() { cout<<"\nClass destroyed !!\n"; } void EvenNullPointerCanCallme(int a); }; void A::EvenNullPointerCanCallme(int a) { int *aa=new int[100]; cout<<"\nHow is that possible !!\n"; delete []aa; } int main(int argc, char* argv[]) { A *nullPtr=NULL;//see i made it NULL nullPtr->EvenNullPointerCanCallme(2);//How daring i am :) delete nullPtr;//I cannot call destructor ! return 0; } Are member functions statically linked with the Object name !! Actually i never found any concept of accessing Class member function with NULL pointers in Any of C++ books Which I have read (Plz let me know if u people have found it in any book !). I am grateful to all u peoples who had a look at my program !! Now see i am getting famous or what :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
-
HI All ,:) I am a VC++ programmer,and some time My Acumen ask Microsofts VC++ (Visual Studio VC++ 6.0 )complier to complile my senseless programs ! :) See what I have asked this time to compile !!.. This is console based program, in which i am trying to call Member function of My class With a NULL pointer . And it is working fine....:cool: Please some one explain this to me ....is this happening of coz Compiler is fed up with my programs !! :-D #include "stdafx.h" #include class A { public : int var; A() { cout<<"In Consrtuctor !!"; } ~A() { cout<<"\nClass destroyed !!\n"; } void EvenNullPointerCanCallme(int a); }; void A::EvenNullPointerCanCallme(int a) { int *aa=new int[100]; cout<<"\nHow is that possible !!\n"; delete []aa; } int main(int argc, char* argv[]) { A *nullPtr=NULL;//see i made it NULL nullPtr->EvenNullPointerCanCallme(2);//How daring i am :) delete nullPtr;//I cannot call destructor ! return 0; } Are member functions statically linked with the Object name !! Actually i never found any concept of accessing Class member function with NULL pointers in Any of C++ books Which I have read (Plz let me know if u people have found it in any book !). I am grateful to all u peoples who had a look at my program !! Now see i am getting famous or what :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
It works because: * you are not accessing member data (not using 'this') * non-virtual functions are staticly linked by name Make the function virtual, it should fail then. Functions that are in your class but do not access member data, should be marked as static. It'll work the same. And at that point you can access it with: A::EvenNullPointerCanCallme()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [Santa Cruz Networks](http://www.santacruznetworks.com)