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.
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.
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++.
calloc operator,malloc operator
I'm not sure what you mean, but the c assignment operator is the equal sign, =
Most likely the function call (yes, it is an operator in C), but of course it is up to you.
addition operator subtraction operator product
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++.
+ += - -= * *= / /= % %= = == != <= >= & && | ^ ~ << <<= >> >>= , [] () are the basic operator in TURBO C
In C, the sizeof operator can be considered a dummy operator because it does not perform any operations on the data but simply returns the size in bytes of a variable or a data type.
conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++
:: operator can not be used in C.
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.
No. Operator and/or function overloading is only a C++ thing.
Conditional Operator- Its the only ternary operator in c/c++.- Its syntax is-(condition)?statement1:statement2;-Shruti Jain
The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.
The purpose of a method statement is that it provides us with the details of what the required operator is supposed to do.
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 }
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.