--%>

"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 : Differentiate between MFC and Win32

    Differentiate between MFC and Win32?

  • Q : Microsoft Access I have attached a

    I have attached a database and a PDF with 5 questions. I need the 5 questions answered with 5 queries in the database. Question 3 needs a crosstab query. All the questions require the proper join.

  • 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 : Define Hexadecimal Hexadecimal : Number

    Hexadecimal: Number representation in hexadecimal is base 16. In base 16, the digits 0-9 and the letters A to F are utilized. A symbolizes 10 (base 10), B symbolizes 11 (base 10), and so forth. Digit positions symbolize successive pow

  • Q : What is an Exception Exception : An

    Exception: An object symbolizing the occurrence of an exceptional circumstance - usually, something which has gone wrong in the smooth running of a program. Exception objects are made from classes which expand the Throwable class.

  • Q : What is Reflection Reflection : It is

    Reflection: It is the ability to find out what fields, methods, constructors, and so forth, are stated for an object or class. Reflection is supported by the Class ‘class’, and other classes in the java.lang.reflect package. Reflection mak

  • Q : Explain myriad of computer systems of

    Explain myriad of computer systems of your target users?

  • Q : Explain why java is so important for

    The internet aided java to the forefront of programming. And java consequently has had a deep effect on the internet. The reason for this is highly simple: java uses the universe of objects that can travel freely in cyber space. In a network, two broad categories of

  • Q : What is the use of compatibility testing

    What is the use of compatibility testing?

  • Q : Search and Coverage of SPIN Search and

    Search and Coverage of SPIN: SPIN has a highly optimized state exploration algorithm. It supports random, interactive and guided simulation, and both exhaustive and partial coverage, based on either depth-first or breadth-first search.

    Discover Q & A

    Leading Solution Library
    Avail More Than 1443327 Solved problems, classrooms assignments, textbook's solutions, for quick Downloads
    No hassle, Instant Access
    Start Discovering

    18,76,764

    1949485
    Asked

    3,689

    Active Tutors

    1443327

    Questions
    Answered

    Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!

    Submit Assignment

    ©TutorsGlobe All rights reserved 2022-2023.