The this Pointer

All nonstatic member functions can use the this keyword, which is a const (non-modifiable) pointer to the object for which the function was called. Member data is addressed by evaluating the expression this–>member-name (although this technique is seldom used). In member functions, using a member name in an expression implicitly uses this–>member-name to select the correct function or data member.

Note:

Because the this pointer is nonmodifiable, assignments to this are not allowed. Earlier implementations of C++ allowed assignments to this.

Occasionally, the this pointer is used directly—for example, in manipulation of self-referential data structures, where the address of the current object is required.

Type of this Pointer

The this pointer's type can be modified in the function declaration by the const, volatile, __near, __far, and __huge keywords. To declare a function as having the attributes of one or more of these keywords, use the cv-mod-list grammar which follows. (For more information, see “Memory-Model Modifiers and Member Functions” in Appendix B, on topic .)

Syntax

cv-mod-list:
cv-qualifier cv-mod-list
opt
pmodel cv-mod-listopt

cv-qualifier:
const
volatile

Microsoft Specific

pmodel:
__near
__far
__huge¨

Consider this example:

class Point

{

unsigned X() const;

};

The preceding code declares a member function, X, in which the this pointer is treated as a const pointer to a const object. Combinations of cv-mod-list options can be used, but they always modify the object pointed to by this, not the this pointer itself. Therefore, the following declaration declares function X; the this pointer is a const pointer to a const object that is addressed far (using intersegment addressing):

class Point

{

unsigned X() __far const;

};

The type of this is described by the following syntax, where cv-qualifier-list can be const or volatile, class-type is the name of the class, and imodel is a memory-model option:

cv-qualifier-listoptclass-type imodelopt* const this

Table 8.2 explains more about how these modifiers work.

Table 8.2 Semantics of this Modifiers

Modifier Meaning

const Cannot change member data; cannot invoke nonconst member functions.
volatile Member data is loaded from memory each time it is accessed; disables certain optimizations.
__near Addressing is intrasegment (16-bit)(Microsoft specific).
__far Addressing is intersegment; pointer arithmetic is 16-bit. Individual objects must be smaller than 64K (Microsoft specific).
__huge Addressing is intersegment; pointer arithmetic is 32-bit (use __huge for large arrays of large objects) (Microsoft specific).

For objects explicitly declared as const, it is an error to call nonconst member functions. Similarly, for objects explicitly declared as volatile, it is an error to call nonvolatile member functions. This model is followed for __near, __far, and __huge.

Member functions declared as const cannot change member data—in such functions, this is a pointer to a const object.

Note:

Constructors and destructors cannot be declared as const or volatile. They can, however, be invoked on const or volatile objects. Constructors and destructors can be declared as __near, __far, or __huge; which constructor or destructor is called depends on the addressing of the object being created or destroyed.