answersLogoWhite

0

Every instance of a class inherits a 'this' pointer. It always points to the instance itself. Outside of the object you must use the object's variable name to refer to the object, or instantiate a pointer to the object. But from within the object's member methods you must use the 'this' pointer which is instantiated automatically as soon as the object is constructed and falls from scope when the destructor returns. Only non-static member functions have access to the 'this' pointer.

There are several uses, however the most important is when checking for self-references, particularly in the assignment operator overload. That is, any member function that accepts a reference to the same class of object should always check for self-references before attempting to mutate the instance. This is particularly important when the class "owns" memory that is dynamically allocated to it.

It is also used to return a reference to the current instance from the assignment operator and from any other operator overload or function that must return a reference to the current instance (including the addition and subtraction operators). Both uses can be seen in the following stripped-down example:

class MyObject

{

public:

// Assignment operator overload.MyObject& operator= ( const MyObject & obj ){

// Self-reference check.

if( this != &obj ){// Assignment code goes here...

}

// Return a reference to this object.

return( *this );

}

};

The 'this' pointer can also be used to pass a pointer (this) or reference (*this) to external functions that accept such arguments.

It should also be noted that when referring to an instance member from within a non-static member function, the dereferenced 'this' pointer is implied, as in:

this->[member_name]

Although this usage is never required, there may be times when it can help make your code more readable, or less ambiguous, especially when a member function must handle one or more external instances of the same class.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

What is the operator that cannot be overloaded?

There are 5 operators which cannot be overloaded. They are: * .* - class member access operator * :: - scope resolution operator * . - dot operator * ?:: - conditional operator * Sizeof() - operator Note:- This is possible only in C++.


What are the basic operator in turbo c?

+ += - -= * *= / /= % %= = == != <= >= & && | ^ ~ << <<= >> >>= , [] () are the basic operator in TURBO C


What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


Use of scope resolution operator in C programming?

:: operator can not be used in C.


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


What is condional operator?

Conditional Operator- Its the only ternary operator in c/c++.- Its syntax is-(condition)?statement1:statement2;-Shruti Jain


What is the purpose of a method statement and what is required of the operator?

The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.


What is the purpose of method statement and what is required of the operator?

The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


Which is dummy operator in c?

unary + is the only dummy operator in c,...


What is THIS operator in c?

The this operator is not a c operator. It is a c++ keyword. It is equivalent to an r-value pointer to the current instance of an object. It is useful when resolving between object members and method parameters.