--%>

"This" Pointer

"this" pointer: In C++ uses a unique keyword called "this" to represent a object that invokes a member function. This is a pointer that directs to the object for which the function was called. For which for example the function call A max () will set the pointer this is to the address of the object A. The starting address is similar to the address of the first variable in the class arrangement. This unique pointer is by itself passing on to a member function when it is called. The pointer this acts like an implicit argument to all the member functions. Consider the following easy instance:

Class A B C

{

Int a;

...

...

};

The private variable a is called can be used directly inside a member function, like.

A = 123;

We can also used the following statement to do the same job:

This - > a = 123;

Since C++ permits the use of shorthand form a = 123. We have not been using the pointer openly so far. However, we have been implicitly using the pointer this when overloading the operators using member functions. Recall that, when a binary operator using member functions, we pass only argument to the function. The other argument is absolutely passed using pointer this. One important application of the pointer this is to return the object it to points to. For example, the statement

Return * this;

Inside a member function will return the object that invoked the function. This statement assumes importance when we want to compare two or more objects inside a member function and return the invoking object as a result:

Person & person : : greater (person & x)    

                                                                    {

If x. Age > age

Return x;      // argument object

Else

Return * this;     // invoking object

}

Suppose we invoke this function by the call

Max = A. Greater (B);

The function will return the object B (argument object) if the age of the person B is greater than of A, otherwise, it will return the object A (invoking object) using the pointer this. T, the differences operator * produces the contents at the address contained in the pointer.

Member conversion function: Conversion from a class type is frequently achieved using conversion functions. Conversion functions use the syntax given below:

Grammar  

Conversion _ function - name:

Operator conversion _ type - name ()

Conversion _ type - name:

Type _ specifier _ list ptr _ operator

The following example specifies a conversion function that converts

Type memory to type double:

// special _ conversion _ function 1. Cpp

Structural money

{

Operator double ()

{

Return _ amount;

}

Private:

Double _ amount;

};

Int main ()

{

Money account;

Double cashon hand = account;

}

The initialization of cashon hand with account causes a conversion from type account to type double. Conversion functions are frequently known as cast operators, as they (along with constructors) are the functions called when a cast is used. The following instance utilizes a cast or explicit conversion, to print the present value of an object of type money.

Cout << (double) account << end l,

Conversion functions are inherited in derived classes. Conversion operators hide an only base class conversion operator that converts to exactly the same type. Therefore, a user defined operator Int functions does not hide a user defined operator short function in a base class. Only one user defined conversion function is applied when performing implicit conversions. If there is no explicitly defined conversion function, the compiler does not look for intermediate types into which an object can be converted. If a conversion is required that causes as ambiguity, an error is generated. Ambiguities arise when more than one user defined conversion is available or when a user defined conversion and a built in conversion exist.

Example: The following example illustrates a class declaration with a potential ambiguity:

// special _ conversion _ functions 2 . cpp

# include < string. h >

  # include < stdio. h >

Structural string

{

// define constructor that converts from type character

String (char * sz Buffer, size _  t size of buffer )

{

If (size of buffer > size of (sz buffer) )

Return;

Structural copy _ S ( _ text, size of buffer, sz buffer );

// define conversion of type char

Operator char * ()

{

Return _ text;

}

Int operator = = (constant string & s)

{

Return ! structural cmp ( _ text, s _ text);

}

Private:

Char _ text [80];

};

Int main ()

{

String s ("a, b, c, d \0", 5);

Char _ ch = "e, f, g, h";

// cause the compiler to select a conversion,

Return s = = ch;

}

In the expression s = = ch the compiler has two choices and no way of determining which is correct. It can convert ch to an object og type string using the constructor and then perform the comparison and then perform the comparison using the user defined operator = =, or it can converts s to a pointer of type char * using the conversion function and then it perform a comparison of the pointers. As none of the choice is "more correct" than another, the compiler cannot identify the meaning of the comparison expression, and it produces an error.

   Related Questions in Programming Languages

  • Q : Monte Carol method to estimate pi An

    An interesting way to estimate pi can can done using Monte Carol method with minimal mathematics. Monte Carlo is known for its casinos. A Monte Carlo method uses change, or in our case, random numbers to simulate the real situation. The situation we simulate here is t

  • Q : Explain AJAX Control Extender Toolkit

    Explain the AJAX Control Extender Toolkit.

  • Q : What is BLAST in program model checking

    What is BLAST in program model checking: The abbreviation is Berkeley Lazy Abstraction Software Verification Tool (BLAST) is a software model checker for C programs. The main goal of BLAST (BLAST website) is to be able to check that software satisfies

  • Q : Define the term Binary Binary : This is

    Binary: This is the number representation in base 2. In base 2, only digits 0 and 1 are utilized. Digit positions symbolize successive powers of 2.

  • Q : Maximum clock frequency of the circuit

    What do you mean by the term set up time and hold time constraints? Explain what do they mean? Which one is vital for estimating the maximum clock frequency of the circuit?

  • Q : Define Number of Interleavings Number

    Number of Interleavings: Besides the raw number of threads, the state space is affected by the number of potential interleavings of these threads. While there exist automated techniques (partial-order reduction) to reduce these interleavings, most mod

  • Q : Explain Operating system Operating

    Operating system: The operating system permits a computer's hardware devices to be accessed by the programs. For example, it permits data to be managed on a computer's disks in the form of a file system and it delivers the co-ordinate positions of a m

  • Q : State the term URN State the term URN?

    State the term URN?

  • Q : Program for Linux operating system that

    Write a program for Linux operating system that uses Posix threads to sort a large array.  Specifically, you should write a program to generate random numbers. Then create at least two threads, each of which sorts a portion of the array. After the thr

  • Q : What is Round robin allocation Round

    Round robin allocation: It is an allocation of time slices which repeatedly cycles regarding a set of eligible threads in the fixed order.