Thread: anyone help me on usage of this

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    anyone help me on usage of this

    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~

  2. #2

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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~

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    i think *this will do same affect but my compiler still report an error when i try to do that ??? here is my code:
    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();
    }
    any ideas ?
    Last edited by black; 05-24-2004 at 04:16 AM.
    Never end on learning~

  5. #5
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    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();
    }

  6. #6
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    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:
    Code:
    int i = 5;
    int* ptr = &i;
    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:
    cout << i << endl;
    cout << *ptr << endl;

  7. #7
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by Carlos
    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 =")
    Do you have an example?

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx guys ! i think i got it !
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. Win2K Limiting CPU Usage?
    By drdroid in forum Tech Board
    Replies: 4
    Last Post: 03-31-2004, 02:08 PM