--%>

"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 : What is Runtime stack Runtime stack :

    Runtime stack: It is a stack structure maintained by the Java Virtual Machine which records that methods are presently being executed. The most of late entered technique will be at the top of the stack and the main technique of an application will be

  • Q : Long lock in the tibco iProcess

    Describe the term Long lock in the tibco iProcess in brief.

  • Q : Programming with C# QUESTION 1      

    QUESTION 1       The following UML diagram describes an abstract class Customer. This class is to be used as part of a Company's inventory system. The inventory system will contain many different types of customers.  A separate s

  • Q : What is Character set encoding

    Character set encoding: The set of values allocated to characters in a character set. Associated characters are frequently grouped with consecutive values, like the digits and alphabetic characters.

  • Q : What is an Anonymous array Anonymous

    Anonymous array: It is an array formed without an identifier. The anonymous array is generally formed as an actual argument, for example:// generate an anonymous array of integers.    YearlyRainfall y2k = new YearlyRai

  • Q : What is Timeslice Timeslice : It is the

    Timeslice: It is the amount of running time assigned to a process or thread prior to the scheduler considers the other to be run. The process or thread will not be capable to employ its full allocation of time when it becomes blocked or preempted thro

  • Q : Explain script recorded over tested web

    Why is the script recorded over the tested web pages not playback correctly?

  • Q : What is Disk drive Disk drive : It is a

    Disk drive: It is a hardware device employed to store the data. They come in numerous forms, like floppy disks, compact disks, and hard disks.

  • Q : Define the term Sound card Define the

    Define the term Sound card: It is a hardware device employed to turn digital data into sound.

  • Q : Define Class Class : It is a

    Class: It is a programming language concept which permits data and techniques to be grouped altogether. The class concept is basic to the notion of an object-oriented programming language. Methods of a class define the set of permitte