hi all~
Below is the description of keyword this on my book:
this points to the invokinig object and *this is the invoking object itself
with these marks this and *this are the same ? anyone could explain differnece between them ? thanx !!!
Printable View
hi all~
Below is the description of keyword this on my book:
this points to the invokinig object and *this is the invoking object itself
with these marks this and *this are the same ? anyone could explain differnece between them ? thanx !!!
thanx did that mean if we can reference a member like this: this->someMember then we can reference it with this way too: *this.someMember ?
i think *this will do same affect but my compiler still report an error when i try to do that ??? here is my code:
any ideas ?Code:#include <iostream>
class Test
{
public:
int n;
Test() { n = 0; };
Test(int m) { n = m; };
int getInt() { return *this.n; }; // Error ???
};
int main()
{
Test t1(9);
std::cout << t1.getInt();
}
Code:#include <iostream>
class Test
{
public:
int n;
Test() { n = 0; };
Test(int m) { n = m; };
int getInt() { return (*this).n; }; // it's a pointer
//int getInt() { return this->n; }; // this works also
};
int main()
{
Test t1(9);
std::cout << t1.getInt();
}
Just some hints:
- don't missuse the this pointer (as you actually do); it's not meant for such purposes, rather for self-checking (e.g. in the case of user-defined "operator =")
- understand the meaning of dereference operator *
If you have a pointer, say:
whereas the pointer ptr has the address of i, you can access i also through the ptr pointerusing the dereference operator. Following lines will display the same value (5):Code:int i = 5;
int* ptr = &i;
Code:cout << i << endl;
cout << *ptr << endl;
Do you have an example?Quote:
Originally Posted by Carlos
Code:someClass& someClass::operator=(const someClass & rhs)
{
if (this==&rhs)
return *this;
//...
}
thanx guys ! i think i got it ! :D