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 !!!
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 !!!
Never end on learning~
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 ?
Never end on learning~
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(); }
Last edited by black; 05-24-2004 at 04:16 AM.
Never end on learning~
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;
[R]evolution!
Programming related articles, downloads, demos
Code:someClass& someClass::operator=(const someClass & rhs) { if (this==&rhs) return *this; //... }
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
thanx guys ! i think i got it !
Never end on learning~