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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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