title: Casting a pointer to the wrong type and calling functions on it c++
body: <p>I'm learning about pointers to objects. I have this code (the comments in main reflect the output for each statement). </p>
  
  <p>Foo stores a void pointer to an object, and checks to see if other pointers to that object are either itself, or reference the same object pointed to by its stored pointer.</p>
  
  <p>When checking objects referenced by the stored pointer, it first casts that pointer to type bar.</p>
  
  <p>In the last few statements, it takes a void pointer to an object of type foo, casts the pointer to object of type bar, and then calls a function on it which only exists in bar. Why does the bar function work when the object is type foo?</p>
  
  <pre><code>#include &lt;iostream&gt;
  //#include &lt;random&gt;
  //#include "sqlite3.h"
  //#include "SDL.h"
  
  //#include &lt;cstdlib&gt;
  
  //#include &lt;algorithm&gt;
  //#include &lt;stdio.h&gt;
  
  using namespace std;
  //int RunSqlNoCallback(sqlite3 * db, const char * zSql);
  
  
  class bar
  {
      public:
          bar()
          {
              x = 0;
          }
  
          void checkSelf(void* somePointer)
          {
              if (somePointer == this)
              {
                  printf("bar it's me!\n");
              }
              else
              {
                  printf("bar It's not me.\n");
              }
          }
          void setx(int y)
          {
              x = y;
          }
          void checkx()
          {
              printf("This bar has x = %i\n",x);
          }
  
      private:
          int x;
  };
  
  class foo
  {
      public:
          void checkSelf(void* somePointer)
          {
              if (somePointer == this)
              {
                  printf("foo it's me!\n");
              }
              else
              {
                  printf("foo It's not me.\n");
              }
          }
  
          void setsuch(void* someptr)
          {
              somesuch = someptr;
          }
  
          void checksuch(void* someptr)
          {
              bar* foobar = (bar*) (somesuch);
              foobar-&gt;checkSelf(someptr);
          }
  
          void checksuchx()
          {
              bar *foobar = (bar*) (somesuch);
              foobar-&gt;checkx();
          }
  
      private:
          void* somesuch;
  };
  
  int main(int argc, char *argv[])
  {
  
      foo myfoo;
      foo notmyfoo;
      bar mybar;
  
      mybar.checkx(); //this bar has x = 0
      mybar.setx(5);
      mybar.checkx(); //this bar has x = 5
  
      foo* myfooptr = &amp;myfoo;
      bar* mybarptr = &amp;mybar;
  
      myfoo.checkSelf(myfooptr); //foo it's me
      myfooptr = &amp;notmyfoo;
      myfoo.checkSelf(myfooptr); //foo it's not me
      myfoo.checkSelf(mybarptr); //foo it's not me
      myfooptr = &amp;myfoo;
      myfoo.checkSelf(myfooptr); //foo it's me
  
      myfoo.setsuch(mybarptr);
      myfoo.checksuch(mybarptr); //bar it's me
      myfoo.checksuchx(); //this bar has 5
      mybar.setx(7);
      myfoo.checksuchx(); //this bar has 7
      myfoo.checksuch(myfooptr); //bar it's not me
      myfoo.setsuch(myfooptr);
      //!This next line is the most confusing one. Why does it say "bar" in the output when foo has the same function "checkself" called by "checksuch"?
      myfoo.checksuch(mybarptr); //bar it's not me
      myfoo.checksuch(myfooptr); //bar it's not me
      myfoo.checksuchx(); //this bar has x=8715752
      system("PAUSE");
      return 0;
  }
  </code></pre>