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. Null Pointer Function Call

Null Pointer Function Call

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelptutorialquestion
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
    ajmalsiddiqi
    wrote on last edited by
    #1

    Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006

    S C D 3 Replies Last reply
    0
    • A ajmalsiddiqi

      Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      This works because:  - The member function doesn't access any non-static members and thus doesn't use the (NULL) this pointer; and  - Isn't virtual so doesn't use this this to get a vtable pointer during function call. Steve

      1 Reply Last reply
      0
      • A ajmalsiddiqi

        Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        It is because classes don't have their own functions (they is only one function for each instances of the classes, and there is an implicit 'this' parameter that is passed to the function to identify the instance). Thus, the function exists even if the object doesn't exist. Now, if this function will access one of the member variable from the class, you will get an exception (because member variables are not 'shared') because you are trying to access invalid memory.

        1 Reply Last reply
        0
        • A ajmalsiddiqi

          Plz See the following Example first. Class A { Void Func(){AfxMessageBox("Function Class A");} } int Main() { A *pClassA; pClassA=NULL; pClassA->Func(); } The output is a MessageBox displayed: AfxMessageBox("Function Class A"); Why this output is displayed because pointer has no memory plz explain this behaviour in the context of VC++ 6.0/MFC.Is this compliler Bug or My VC++6.0 is corrupt or any other.....? Yours Sincerely Ajmal Siddiqi ajmalsiddiqui -- modified at 6:45 Monday 8th May, 2006

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          If you look at the assembly code, you'll notice that the call to Func() is the same no matter if pClassA is NULL or not.


          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

          "We will be known forever by the tracks we leave." - Native American Proverb

          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