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.
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.
The 'this' pointer refers to whatever instance of the class you are working with.
If you create an instance:Code:class Foo { public: Foo( ) { x = 10; } void Bar( ) { cout << this->x; } private: int x; };
Foo FooBar;
The "this" pointer refers to FooBar.
Quzah.
Hope is the first step on the road to disappointment.
Thanks Quzah.
What about in the following code:
"This" refers to "other". Is that right?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); }
Last edited by kes103; 07-23-2003 at 10:19 PM.
>> "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; }
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 07:45 PM.
Think of an ordinary pointer, and how it is dereferenced to obtain the value it points to:
int num = 10;
int * ptr = #
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; }
No. Say I have this: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.
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.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. }
Last edited by Cat; 07-25-2003 at 10:38 PM.
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?
Not quite...
this is a pointer. *this is the object
Both of these exist for any member function, not just operator=().
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 +=."
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.Code:Rational& Rational :: operator += (const Rational &r) { num_ = r.num_ + denom_ + r.denom_ * num_; denom_ *= r.denom_; reduce(); return *this; }
Also, thanks, Sebastiani for your post. I didn't see it before.
Last edited by kes103; 07-26-2003 at 11:26 AM.
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.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.If the 'this pointer' is a variable that refers to the current object within a class, then what is the current object?
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 02:04 PM.
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).