Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can Anyone Explain this !! :)

Can Anyone Explain this !! :)

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiolearning
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abhishek Srivastava
    wrote on last edited by
    #1

    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 :)

    M 1 Reply Last reply
    0
    • A Abhishek Srivastava

      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 :)

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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."

      A 1 Reply Last reply
      0
      • M Michael Dunn

        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."

        A Offline
        A Offline
        Antti Keskinen
        wrote on last edited by
        #3

        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.

        R 1 Reply Last reply
        0
        • A Antti Keskinen

          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.

          R Offline
          R Offline
          Roger Allen
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups