Results 1 to 10 of 10

What is "this" pointer?

This is a discussion on What is "this" pointer? within the Programming forums, part of the Web Designing & Development category; The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It ...

  1. #1
    Senior Member chinmay.sahoo's Avatar
    Join Date
    Dec 2013
    Location
    Banglore
    Posts
    266

    Thumbs up What is "this" pointer?

    The this pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer

    When a nonstatic member function is called for an object, the address of the object is passed as a hidden argument to the function. For example, the following function call

    myDate.setMonth( 3 );
    can be interpreted this way:
    setMonth( &myDate, 3 );


    The object's address is available from within the member function as the this pointer. It is legal, though unnecessary, to use the this pointer when referring to members of the class.

  2. #2
    Member
    Join Date
    Dec 2015
    Location
    Guangzhou,China,51000
    Posts
    85

    Default Re: What is "this" pointer?

    The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions.

  3. #3
    Junior Member
    Join Date
    Apr 2017
    Posts
    10

    Default Re: What is "this" pointer?

    Hello! Very interesting topic, thank you raised it

  4. #4
    Junior Member
    Join Date
    Apr 2017
    Posts
    8

    Default Re: What is "this" pointer?

    ‘this’ pointer in C++
    The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
    For a class X, the type of this pointer is ‘X* const’. Also, if a member function of X is declared as const, then the type of this pointer is ‘const X *const’ (see this GFact)


    Following are the situations where ‘this’ pointer is used:


    1) When local variable’s name is same as member’s name


    #include<iostream>
    using namespace std;

    /* local variable is same as a member's name */
    class Test
    {
    private:
    int x;
    public:
    void setX (int x)
    {
    // The 'this' pointer is used to retrieve the object's x
    // hidden by the local variable 'x'
    this->x = x;
    }
    void print() { cout << "x = " << x << endl; }
    };

    int main()
    {
    Test obj;
    int x = 20;
    obj.setX(x);
    obj.print();
    return 0;
    }
    Run on IDE
    Output:


    x = 20
    For constructors, initializer list can also be used when parameter name is same as member’s name.

    Ethan Stark

  5. #5
    Junior Member
    Join Date
    Apr 2017
    Posts
    8

    Default Re: What is "this" pointer?

    ‘this’ pointer in C++
    The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
    For a class X, the type of this pointer is ‘X* const’. Also, if a member function of X is declared as const, then the type of this pointer is ‘const X *const’ (see this GFact)


    Following are the situations where ‘this’ pointer is used:


    1) When local variable’s name is same as member’s name


    #include<iostream>
    using namespace std;

    /* local variable is same as a member's name */
    class Test
    {
    private:
    int x;
    public:
    void setX (int x)
    {
    // The 'this' pointer is used to retrieve the object's x
    // hidden by the local variable 'x'
    this->x = x;
    }
    void print() { cout << "x = " << x << endl; }
    };

    int main()
    {
    Test obj;
    int x = 20;
    obj.setX(x);
    obj.print();
    return 0;
    }

  6. #6
    Junior Member
    Join Date
    Apr 2017
    Posts
    8

    Default Re: What is "this" pointer?

    very object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.


    Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.


    Let us try the following example to understand the concept of this pointer:


    #include <iostream>

    using namespace std;


    class Box {
    public:
    // Constructor definition
    Box(double l = 2.0, double b = 2.0, double h = 2.0) {
    cout <<"Constructor called." << endl;
    length = l;
    breadth = b;
    height = h;
    }

    double Volume() {
    return length * breadth * height;
    }

    int compare(Box box) {
    return this->Volume() > box.Volume();
    }

    private:
    double length; // Length of a box
    double breadth; // Breadth of a box
    double height; // Height of a box
    };


    int main(void) {
    Box Box1(3.3, 1.2, 1.5); // Declare box1
    Box Box2(8.5, 6.0, 2.0); // Declare box2


    if(Box1.compare(Box2)) {
    cout << "Box2 is smaller than Box1" <<endl;
    } else {
    cout << "Box2 is equal to or larger than Box1" <<endl;
    }

    return 0;
    }

  7. #7
    Junior Member
    Join Date
    Apr 2017
    Posts
    8

    Default Re: What is "this" pointer?

    I would like to read about it anymore. Prompt, what literature to study?

  8. #8
    Senior Member
    Join Date
    Mar 2017
    Posts
    233

    Default Re: What is "this" pointer?

    pointer is a variable which holds the address of other variable.

  9. #9
    Senior Member charlottegracie's Avatar
    Join Date
    Jan 2017
    Posts
    207

    Default Re: What is "this" pointer?

    Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.

  10. #10
    Senior Member
    Join Date
    May 2017
    Posts
    477

    Default Re: What is "this" pointer?

    this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.

  11. #11
    Senior Member
    Join Date
    Nov 2016
    Posts
    152

    Default Re: What is "this" pointer?

    Good..Short and simple.

    Quote Originally Posted by Sreesri View Post
    this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.

  12. #12
    Member
    Join Date
    Jul 2017
    Posts
    63

    Default Re: What is "this" pointer?

    The this keyword is used to represent an object that invokes the member function. It points to the object for which this function was called. It is automatically passed to a member function when it is called.

    - When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object . This pointer is known as 'this' pointer. It is internally created at the time of function call.
    - The this pointer is very important when operators are overloaded.

  13. #13
    Member
    Join Date
    Mar 2018
    Location
    Santa Clara, California, USA
    Posts
    40

    Default Re: What is "this" pointer?

    The (this) keyword is one of the most widely used and yet confusing keywords in JavaScript. (this) keyword points to a particular object. Now, which is that object depends on how a function which includes 'this' keyword is being called.