--%>

"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 : How much does Symbian Signed

    How much does Symbian Signed certification and testing cost? Answer: Test houses contain their own prices for Symbian Signed testing. So you can check that prices through searching over the internet.

  • Q : Function overloading in C plus Function

    Function overloading in C++: The function name containing numerous definitions which are differentiable by the number or kinds of their arguments is termed as function overloading.

  • Q : Explain Polymorphism Polymorphism : It

    Polymorphism: It is the ability of an object reference to be employed as if it referred to an object with various forms. The polymorphism in Java outcomes from both class inheritance and interface inheritance. The actually different forms frequently o

  • Q : Define the term Toggle Define the term

    Define the term Toggle: To alternate among two values, like true and false, on and off, or 1 & 0.

  • Q : Define Application Application : It is

    Application: It is frequently used, simply as a synonym for the program. Though, in Java, the word is particularly employed of programs with a Graphical User Interface (GUI) which are not applets.

  • Q : Define the term Scheduler Define the

    Define the term Scheduler: The portion of the Java Virtual Machine (abbreviated as JVM) which is responsible for managing the threads.

  • Q : Explain Infinite loop Infinite loop :

    Infinite loop: The loop whose termination test never computes to false. At times this is a deliberate act on the portion of the programmer, employing a construct like:         whi

  • Q : What is Priority level Priority level :

    Priority level: Each and every thread has a priority level that point out to the scheduler where it must be placed in the pecking order for being run. The eligible un-blocked thread with a specific priority will always be run prior to an eligible thre

  • Q : What is Factory pattern Factory pattern

    Factory pattern: A pattern of class definition which is employed as a generator of instances of other classes. Frequently employed to form platform- or locale-particular implementations of abstract classes or interfaces. This decreases coupling betwee

  • Q : Write a program to display its negative

    Write a program in object code that reads a single digit decimal number and displays its negative in binary.  To do this, you must first read the number as a character and then convert it to its numeric value, as discussed in class.  Then, you're going to change this to a negative numbe