this pointer

This is a discussion on this pointer within the C++ Programming forums, part of the General Programming Boards category; If the 'this pointer' is a variable that refers to the current object within a class, then what is the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    this pointer

    If the 'this pointer' is a variable that refers to the current object within a class, then what is the current object? Is it the last method or function within the class? Would appreciate explanations with examples please.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The 'this' pointer refers to whatever instance of the class you are working with.
    Code:
    class Foo
    {
        public:
            Foo( ) { x = 10; }
            void Bar( ) { cout << this->x; }
    
        private:
            int x;
    };
    If you create an instance:

    Foo FooBar;

    The "this" pointer refers to FooBar.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Thanks Quzah.
    What about in the following code:
    Code:
    //copy constructor
    Person::Person (Person const &other)
    {
        //unconditionally copy other's data
       copy(other);
    }
    //destructor
    Person::~Person()
    {
        //unconditionally deallocate
        destroy();
    }
    //overloaded assignment
    Person const &Person :: operator=(Person const &other)
    {
        //only take action if no auto-assignment
        if(this != &other);
       {
             destroy();
             copy(other);
       }
        // return (reference to) current object for
        // chain-assignments
        return (*this);  
    }
    "This" refers to "other". Is that right?
    Last edited by kes103; 07-23-2003 at 11:19 PM.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> "This" refers to "other". Is that right?

    No. As stated, the 'this' pointer was invented for the necesity of referring to the object itself when writing member function code. Nothing else.

    Now when you see:

    >> if(this != &other)

    ...you are just verifying that something like this didn't happen:

    Person x;

    x = x;

    That's it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    OK, after more reading of the book the code came from and others and after some more pondering...
    'this' refers to 'operator' (the current object) and 'other' refers to an actual Person object. '&other' is a reference to or 'alias' to an actual Person object.
    '*this' refers to the left hand operand of the overloaded '=' operator.
    I'll assume this is correct if I'm not contradicted.
    Last edited by kes103; 07-25-2003 at 08:45 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Think of an ordinary pointer, and how it is dereferenced to obtain the value it points to:

    int num = 10;
    int * ptr = &num;
    cout << *ptr << endl; // prints '10'

    So 'this' is a pointer to the current object, and '*this' is the current object.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by kes103
    OK, after more reading of the book the code came from and others and after some more pondering...
    'this' refers to 'operator' (the current object) and 'other' refers to an actual Person object. '&other' is a reference to or 'alias' to an actual Person object.
    '*this' refers to the left hand operand of the overloaded '=' operator.
    I'll assume this is correct if I'm not contradicted.
    No. Say I have this:

    Code:
    class C{
    public:
      int f();
      const C& operator=(const C&);
    };
    
    int main(){
      C obj1, obj2, obj3;
    
      obj1.f(); // inside this call, this points at obj1
      obj3.f(); // inside this call, this points at obj3
      obj2 = obj3; // inside this call, this points at obj2
      obj2.operator=(obj3); // this is exactly the same as the line above.
    
    }
    this is a pointer to the current object. It's the object upon which the method is being called. Operators, functions, etc. are not objects. An object is an instance of a class, just as a traditional variable is an instance of a primitive (int, bool, etc.). In the above code, there are only three objects -- obj1, obj2, and obj3. f() is a method, not an object. Similarly, the operator is not an object either.
    Last edited by Cat; 07-25-2003 at 11:38 PM.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Thank you Cat.
    Allright, then both '*this' and 'this' refer to the left hand operand of the overloaded '=' operater once an instance of it is called.
    Am I correct now?

  10. #10
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Not quite...

    this is a pointer. *this is the object

    Both of these exist for any member function, not just operator=().

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Thanks, ygfperson, but were you aware that I was referring to the code I posted on 7/23/03 and not operator=() in general.
    As another example, consider this code from another book:
    "The definition of += follows below. Notice how *this is used in the return statement. It is the object that will be the left-hand operand of the operator +=."
    Code:
    Rational& Rational :: operator += (const Rational &r)
    {
        num_ = r.num_ + denom_ + r.denom_ * num_;
        denom_ *= r.denom_;
        reduce();
        return *this;
    }
    So, 'this' points to the object that will be the left operand or the current object and '*this' is the object that will be the left-hand operand of the += operater or the current object.
    Also, thanks, Sebastiani for your post. I didn't see it before.
    Last edited by kes103; 07-26-2003 at 12:26 PM.

  12. #12
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    Re: this pointer

    I read the replies in this topic and it was all about operators and complicated code etc. Not sure why you bring operators up. As far as I understand your problem, it's pretty simple to explain "this". The fact that most of the replies are that complicated makes me wonder if I maybe misunderstand your question. Anyway, I'll give it a try.
    If the 'this pointer' is a variable that refers to the current object within a class, then what is the current object?
    The 'this pointer' refers to the current instance (the object) of a class. It does not refer to the class itself nor does it refer to anything "within" a class.

    Since you talk about objects as if you were thinking they were code structures "within" the class, I'll give a short explanation of what the difference between objects and classes is:

    Imagine a class as the blueprint for all objects of the same type. A class defines which methods (hopefully thats the right term in C++) and variables are available to all objects that are built with this particular "blueprint". Each of these object's has a "this pointer" that points to the object itself, therefore every object's "this pointer" points to a different location in memory than the other objects their "this pointers". this is only available from within the objects. (Of course you use it in the classdefinition, but like anything else there, it doesn't hold any information. It's just a piece of the blueprint.)

    So whats the use of of a pointer to oneself?

    There isn't *that* much use for it, but it isn't useless either. Sometimes you just need it.

    This Webpage explains it quite good and gives examples. The first one is pretty cool. Good way to produce code nobody understands and - as a note for cat - ugly code
    Last edited by darksaidin; 07-26-2003 at 03:04 PM.

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I thought the code was decent (for explanation purposes) until page 4. They correctly identify and teach the problems that self-assignment can bring, but they solve the problem in a terrible manner, one which has many problems with exception safety.

    Outside of an error in their very first code (missing ; after the class definition), it doesn't seem badly written. (That and the fact their janitor is earning 60k per year).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21