--%>

"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 : Explain way to open one resistance of

    Explain the way to open just one resistance of vb application (.exe).

  • 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 : Explain SQA personnel regarding

    Do you think that the role of SQA personnel regarding inspections or testing?

  • Q : Maintaining audit trail of product

    The accuracy of product table data is crucial and the Brwebean’s. owner prefers to have an audit file which contains information regarding all DML activity on the BB_PRODUCT table. This information must point out the user id of the user running a DML statement,

  • Q : Various controls of ASP.NET AJAX Name

    Name the various controls of ASP.NET AJAX?

  • Q : Explain the term Web Services

    Explain the term Web Services Description Language.

  • Q : Explain Virtual memory Virtual memory :

    Virtual memory: It is computer will comprise a limited amount of real memory accessible to it. Programs frequently need more memory than the quantity of real memory. Moreover, in a multiprogramming system, various processes will be competing for simil

  • Q : Use of Macros used in windows-dot-h

    What is the use of Macros used in <windows.h> header files?

  • Q : Benefits of the event-delegation model

    Write down some of the benefits of the event-delegation model over event-inheritance model?

  • Q : Define Container Abstractions Container

    Container Abstractions: Abstractions for containers (such as lists, stacks, sets, or queues) may represent just the state of a container—e.g., full or empty—and abstract away from the actual container content. The list operations also need