The this Pointer
The this pointer is a true "life buoy" that saves us from drowning in the stormy ocean of OOP. Using just this, we can determine to which object instance the called function belongs. Since all nonvirtual functions of an object are called directly using the actual address, the object appears to be split at compile time into the functions that compose it. If there were no this pointers, it would be impossible to reconstruct the hierarchy of functions!
Thus, correct identification of this is very important. The only problem is how to distinguish it from pointers to arrays and structures. We identify an object instance using the this pointer; if this points to allocated memory, we are dealing with an object instance. However, by definition, this is a pointer that refers to an object instance. We have a never-ending loop! Fortunately, there is a loophole — a specific code handles the this pointer, allowing us to distinguish it from all other pointers.
Generally, each compiler has its own specifics; I strongly recommend that you study them by disassembling your own C++ programs. However, there are some commonly accepted guidelines for most implementations. Since this is an implicit argument of each member function of a class, it seems reasonable to defer further discussion of its identification until the "Function Arguments" section. Here we'll just give a brief table summarizing the mechanisms of passing this in various compilers:
Table 1: The Mechanisms of Passing the this Pointer
| Function type |
Compiler | default | fastcall | cdecl | stdcall | Pascal
Microsoft The this pointer is The this pointer is The this pointer is
Visual C++ passed through the passed through the passed through the
ECX register. stackas the last stack as the last
argument. argument of a function.
Borland C++ The this pointer
is passed through
Watcom C++ the EAX register.
Tuesday, September 22, 2009
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment