this pointer

This is a discussion on this pointer within the C++ Programming forums, part of the General Programming Boards category; // Listing 10.15 // Copy constructors #include <iostream> using namespace std; class CAT { public: CAT(); // default constructor // ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    this pointer

    // Listing 10.15
    // Copy constructors

    #include <iostream>

    using namespace std;

    class CAT
    {
    public:
    CAT(); // default constructor
    // copy constructor and destructor elided!
    int GetAge() const { return *itsAge; }
    int GetWeight() const { return *itsWeight; }
    void SetAge(int age) { *itsAge = age; }
    CAT & operator=(const CAT &);

    private:
    int *itsAge;
    int *itsWeight;
    };

    CAT::CAT()
    {
    itsAge = new int;
    itsWeight = new int;
    *itsAge = 5;
    *itsWeight = 9;
    }


    CAT & CAT:perator=(const CAT & rhs)
    {
    if (this == &rhs)
    return *this;// which object is this pointing to?
    *itsAge = rhs.GetAge();
    *itsWeight = rhs.GetWeight();
    return *this;
    }


    int main()
    {
    CAT frisky;
    cout << "frisky's age: " << frisky.GetAge() << endl;
    cout << "Setting frisky to 6...\n";
    frisky.SetAge(6);
    CAT whiskers;
    cout << "whiskers' age: " << whiskers.GetAge() << endl;
    cout << "copying frisky to whiskers...\n";
    whiskers = frisky;
    cout << "whiskers' age: " << whiskers.GetAge() << endl;
    return 0;
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    In this line

    whiskers = frisky;

    *this is whiskers. If the equality is true, then it's obviously both whiskers and frisky of them (they are pointers to the same value).

    I'd say make the assignment operator return void, so that only one assignment can be made per line. Returning a value makes things like this possible.

    Code:
    Cat whiskers;
    Cat Joe;
    Cat blue;
    Cat gray;
    
    whiskers = blue = Joe = gray;  // ugly
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Somebody is being reading TYC++21D...

    Anyway, what's so horrible with
    x = y = z;

    I find it rather handy in some rare situations. IMO it is even more readable than
    x = y;
    z = x;
    or
    x = y;
    z = y;
    kooma - t.h@iki.fi

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    so basically it says.....
    If whiskers (this) is the same as frisky (&rhs) then return whiskers(why must whiskers be returned in the first place)...( we assign the values of frisky to whiskers ) and then return the value of whiskers?

    PS: this is probably wrong but it's just to ilustrate how I want to vew this, to get the hang of it, breaking it down into words....make any changes deemed necessary place, and put into words so I can understand it.


    XPS (extra PS):

    Yes I am reading Sams Teach Yourself C++ in 21 days (fourth editions btw).

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    CAT & CATerator=(const CAT & rhs)
    {
    if (this == &rhs)
    return *this;// which object is this pointing to?
    *itsAge = rhs.GetAge();
    *itsWeight = rhs.GetWeight();
    return *this;
    }

    so basically it says.....
    If whiskers (this) is the same as frisky (&rhs) then return whiskers(why must whiskers be returned in the first place)...( we assign the values of frisky to whiskers ) and then return the value of whiskers?
    Yes that is correct. The value of wiskers must be returned so complex statements using = can be evaluated
    here are some examples
    if( (wiskers=frisky) == someothercat )
    or
    somenewcat = wiskers = frisky;

    In that first example, wiskers would be set to the same as frisky, then it would be compared to someothercat. And the comparson would use the return value
    In the second example the return value from copying frisky to wiskers, is then copyed to somenewcat



    That line prevents this from happening

    Cat1 = Cat1;

    What is the point of copying the same data over?
    In a simple class like this there is not a problem, but what if you have a class where dynamic memory is used this can help out
    check out this sample code
    Code:
    class Test
    {
    private:
       char * str;
    
    public:
      Test( char * s )
      {
       str = new char[strlen(s)+1];
       strcpy(str,s);
      }
    
      Test & operator=(const Test & rhs)
      {
       delete[] str;
       str = NULL;
       str = new char[strlen(rhs.str)+1];
       strcpy(str,rhs.str);
       }
    };
    In that code if you did
    Test a("Hello");
    a=a;
    You would have a problem. The =operator would delete the str, but since the rhs variable is the same one that *this points to, rhs.str is now null. So copying the string over will result in a null string. I hope this isint confusing you.

    Basically you need that check in your class if its a more advanced class, that is using memory allocation. Just put it in, it is good style

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I've done quite a bit of programming, in several languages, and I've yet to find a purpose for *this. WHAT IS IT USEFUL FOR!!!

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    elchulo2002's original example gives one use for it and RpiMatty's reply gave the reason. You need to check for self assignment when implementing operator=() if you're using dynamic memory from within your class. You wouldn't want to de-allocate the memory if you're just about to copy from it.

    Whenever you need to know the address of an object from within one of it's methods you'd need to use it.

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    To each their own.

    "Anyway, what's so horrible with
    x = y = z;"

    It looks ugly. It requires some thought from me to see that x and y are being assigned to z, and x is not being assigned to y.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

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