Can Anyone Explain this !! :)
-
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 1.5 Year Old 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 1.5 Year Old Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
It's perfectly fine to call a method through a null pointer, because all the pointer does is establish the value of
this
in the function. If you were to access any member variables in the function though, you would then be dereferencing the null pointer, which would be Bad. --Mike-- Ericahist [updated Oct 26] | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber Actual sign at the laundromat I go to: "No tinting or dying." -
It's perfectly fine to call a method through a null pointer, because all the pointer does is establish the value of
this
in the function. If you were to access any member variables in the function though, you would then be dereferencing the null pointer, which would be Bad. --Mike-- Ericahist [updated Oct 26] | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber Actual sign at the laundromat I go to: "No tinting or dying."In code-wise, what you create is a type 'pointer-to-class-A', which doesn't point anywhere (initialized to NULL). You can place a call to the member function through this pointer in the code, and the code compiles just fine. However, when you run this program, you'll get either an Access Violation, a complete machine crash or nothing, depending on your luck :) Pointer calls, as far as I know, are evaluated at run-time. The compiler is not aware whether the call is possible or not, it can only decide whether it is legal or not. Illegal calls it will detect and promptly state ' is not a member of '. Because the method you specified is a member of Class A, the compiler doesn't warn you. If you removed the NULL setting, it would warn you that you've used a pointer without initializing it. Try running it, and see what happens ^^ :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
In code-wise, what you create is a type 'pointer-to-class-A', which doesn't point anywhere (initialized to NULL). You can place a call to the member function through this pointer in the code, and the code compiles just fine. However, when you run this program, you'll get either an Access Violation, a complete machine crash or nothing, depending on your luck :) Pointer calls, as far as I know, are evaluated at run-time. The compiler is not aware whether the call is possible or not, it can only decide whether it is legal or not. Illegal calls it will detect and promptly state ' is not a member of '. Because the method you specified is a member of Class A, the compiler doesn't warn you. If you removed the NULL setting, it would warn you that you've used a pointer without initializing it. Try running it, and see what happens ^^ :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
What Mike says is right. The member function is not called through the pointer unless its virtual (in which case it de-references the pointer to lookup the vtable pointer). As it knows that your pointer is of type X, it can call any non virtual function on it directly, its just that in C++ the first hidden parameters passed is the
this
pointer. Dereferencing the pointer in any way would cause the access violation. As the example given only had code accessing variables declared on the stack, there would not be any problems. Roger Allen Sonork 100.10016 Death come early, death come late, It takes us all, there is no reason. For every purpose under heaven, To each a turn, to each a season. A time to weep and a time to sigh, A time to laugh and a time to cry, A time to be born and a time to die. Dust to dust and ashes to ashes, And so I end my song.