Thread: One more question on Memory allocation and operators overloading....

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

    One more question on Memory allocation and operators overloading....

    /* Ok I need help with this, I'll show you the main program and the constructor definition*/

    int main()
    {
    String s1("initial test");
    cout << "S1:\t" << s1.GetString() << endl;

    char * temp = "Hello World";
    s1 = temp;
    cout << "S1:\t" << s1.GetString() << endl;

    char tempTwo[20];
    strcpy(tempTwo,"; nice to be here!");
    s1 += tempTwo;
    cout << "tempTwo:\t" << tempTwo << endl;
    cout << "S1:\t" << s1.GetString() << endl;

    cout << "S1[4]:\t" << s1[4] << endl;
    s1[4]='x';
    cout << "S1:\t" << s1.GetString() << endl;

    cout << "S1[999]:\t" << s1[999] << endl;

    String s2(" Another string");
    String s3;
    s3 = s1+s2;
    cout << "S3:\t" << s3.GetString() << endl;

    String s4;
    s4 = "Why does this work?";
    cout << "S4:\t" << s4.GetString() << endl;

    return 0;
    }

    /* I need on this defintion, basically what is *this doing and what is assigned to what in terms of for example "s1" and '"s4" etc.*/

    void String:perator+=(const String& rhs)
    {
    unsigned short rhsLen = rhs.GetLen();
    unsigned short totalLen = itsLen + rhsLen;
    String temp(totalLen);
    unsigned short i;
    for (i = 0; i<itsLen; i++)
    temp[i] = itsString[i];
    for (unsigned short j = 0; j<rhs.GetLen(); j++, i++)
    temp[i] = rhs[i-itsLen];
    temp[totalLen]='\0';
    *this = temp;
    }


    THIS IS NOT THE WHOLE CODE
    Last edited by incognito; 01-21-2002 at 10:04 PM.
    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
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i would be happy to explain this to you but i don't have that much time today... so i will just give a little taste today......

    *this........is a pointer to an object (a function that is making a call)
    for example...

    int Sum = Addition(int One, int Two);

    in this segment you are making a call to function Addition() and passing two parameters and the "result" the return value will be assigned to the "int Sum" .... in this case, inside the body of a Addtition() function, *this ... would be a pointer to the Addition() function itself...


    in your example there's a lot to explain... from operator overloading to temporaries, constructor and copy constructor... and i am sorry i just don't have time today to answer it all...it is a very long discussion....

    but nevertheless keep reading your book.... Jessy Liberty writes pretty good....

    Good luck,

    Regards,
    matheo917

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I pretty much know some about operator overloading, classes, constructors and all that, the problem is that I am a bit confused on the difference of "operator plus" and this "+=" that pretty much my consfusing there, I mean where is the this pointer return to, and things of that nature.
    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.

  4. #4
    Unregistered
    Guest
    Im no authority but "this" is a pointer to the current object (whatever that may be).
    When "this" is returned, it retuns the value of
    whatever has focus at that point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  2. Overloading new/delete with additional arguments
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2004, 06:26 AM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. Memory Allocation :: C/C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2002, 10:38 AM