Tim Peeler on 20 Oct 2003 14:53:02 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] C++/*this


Hi, long time no post.  I have a C++ question, hopefully someone can
direct me to some good documentation.  

I have always understood that the "this" pointer accessible in non-static
member functions of C++ objects (classes, structs or unions) is actually
an implicit argument added by the compiler.

   Example:

         void Object::Function();  
      is actually:
         void Object::Function(Object *this);

      so when you do this:
         Object ob;
         ob.foo();

      it can be thought of as:
         Object ob;
         Object::foo(ob);

This has always made sense to me.  Since there is only one instantiation
of all member functions of an object, and these functions are not stored
as a part of the object, the function must have the *this pointer as an
implicit argument so that the function has access to the object's other
members.  It also makes sense that static member functions, since they
do not need an object from which they are called, don't get the implicit
*this pointer (this would defeat the purpose of making the function
static).


My instructor seems to think otherwise.  Here is the explination I
was given:

   All classes, structs and unions contain a *this pointer that
   references itself.  The object also contains pointers to the object
   member functions, therefore when calling a member function in an
   object the member function has access to the *this pointer.

   Example:

      class Foo {
         private:
            int x;

         public:
            void setx(int);
      };

      Foo::setx(int) is instantiated only once and each object of type
      Foo has a function pointer to setx:

         Basically, breaking down what he said results in an object
         that "looks" like this:

         Foo {
            private:
               int x;
               Foo *this;

            public:
               void (*setx)(int); // this is the pointer to Foo::setx(int);
         };


This makes no sense to me.  First there is the question of how does the
function (which is instantiated only once, which I know to be true) know
which object is "calling" it?  Is there a way for a function (any
function) to "know" what object it was called from other than providing
it with a pointer to that object?

Furthur, a simple sizeof() test shows that this (the instructor's idea
of how an object is created and stored) can't be true unless sizeof()
doesn't return the true size of an object (which would be VERY bad
especially if you tried to combine a malloc() call to get the required
memory instead of new).

By using the simple Foo class above, with only one private data member
(x), and one public member function (void setx(int)), I see that the
sizeof(Foo) is 4 (which happens to be sizeof(int)).  Is my instructor
full of it or am I missing something here?

If I have it wrong, please someone point me to some documentation that
shows me exactly how a class' member functions (non-static) "know" which
object has called them and where they get *this.

Thanks,
Tim


--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d-(+) s:>++:++ a->-- C++++$ UL++++$ P++++$ L++++$ E--- W++(+++)$
N(-) o-- K- w--- !O? M- V-- PS++(+++) PE-- Y++ PGP t+(++) 5 X R tv(--)
b+>+++ DI+++ D+(++) G e+>++>+++>++++ h-- r--->+++ y-->+++
------END GEEK CODE BLOCK------
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug