answersLogoWhite

0

The 'this' pointer is not an operator, it is a special pointer that exists within every instance of a class, and always points to the current instance of that class. It can only be used in non-static methods of the class because static methods do not have a 'this' pointer; static methods can be called even when there is no instance of the class.

Whenever an instance method refers to one of its own members (non-static members), the 'this' pointer is implied:

int CMyObject::foo()

{

return( this->bar ); // returns the bar member of this instance.

}

The 'this' pointer also makes it possible for an instance to compare itself to other instance references (often to ensure they are different instances) as well as to return a reference to itself. The assignment operator cannot be implemented any other way:

CMyObject& CMyObject::operator= (const CMyObject & rhs )

{

if( this != &rhs ) // check for self-reference

bar = rhs.bar; // perform assignment

return( *this ); // return a reference to this instance.

}

The 'this' pointer also allows an instance to pass a reference or pointer to itself to external functions, including the methods of other instances of the same class.

User Avatar

Reynold Berge

Lvl 10
2y ago

What else can I help you with?

Related Questions

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.


What are the duties of switchboard operator?

the purpose of a switch board is a connection for


What is the purpose of using the "phase operator" in quantum mechanics?

The purpose of using the "phase operator" in quantum mechanics is to describe the phase of a quantum state, which is important for understanding interference effects and the behavior of quantum systems.


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 is the purpose of the safety landyard on pwc?

to stop the engine if the operator falls overboard


What is the purpose for a safety lanyard on a PWC?

To shut off the engine if the operator falls overboard.


What is the purpose of a lanyard attached to an ignition safety switch?

to shut off the engine if the operator falls overboard


What is the purpose of a lanyard attached to ignition switch on a boat?

to shut off the engine if the operator falls overboard


What the purpose of a lanyard attached to a ignition safety switch?

to shut off the engine if the operator falls overboard


What is the purpose of a lanyard attached to the safety ignition switch?

to shut off the engine if the operator falls overboard


What is the purpose of a lanyard attached to the ignition safety switch?

to shut off the engine if the operator falls overboard