Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:25:37+00:00 2026-06-03T03:25:37+00:00

I need help with both of my operator overloading functions presented below. I’m unsure

  • 0

I need help with both of my operator overloading functions presented below. I’m unsure of how I can implement this without actually using the assignment in the function definitions.

Code for operator + in my .cpp file:

MyString& MyString::operator +(const MyString& rhs)
{
  delete [] String;
  String = new char[rhs.Size];
  Size = rhs.Size;
  // needs to be a loop for cascading + 
  // so that String1=String2+String3+String4 will work
  for(int i = 0; i < rhs.Size+1 ; i++)
  {
    //  String[i] + rhs.String[i]; ???
  }
  return *this;
}

Code for += operator in .cpp file:

MyString& MyString::operator+=(const MyString& rhs)
{
  delete [] String;
  String = new char[rhs.Size];
  String = String + rhs.String;
  return *this;
}

Call from main.cpp:

 String1 = String2 + String3 + String4;
 String1.Print ();

 String2 += String3;
 String2.Print ();

I know my .cpp file codes are wrong, some insight would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:25:39+00:00Added an answer on June 3, 2026 at 3:25 am

    First, usually you return a new object from operator+, because the expectation is that calling + on an object does not change the object itself.

    MyString MyString::operator+ (const MyString& rhs)  
    {  
      // ...
    
      return MyString(...);  
    
    } 
    

    Note the missing reference (&) from the return type: you are returning the new object by-copy, not by-reference.

    Second, if you delete the String at the beginning, you will not be able to copy its contents. Consider this for operator+:

    char* tmp = new char[Size + rhs.Size + 1]; // +1 for the terminating '\0'      
    for(int i = 0; i < Size ; i++)      
    {
      // copy the contents of current object buffer, char-by-char
      tmp[i] = String[i]; 
    }
    for(int i = 0; i < rhs.Size+1; i++) // +1 to copy the terminating '\0' as well
    {      
      // copy the contents of other object buffer, char-by-char
      tmp[i+Size] = rhs.String[i]; 
    }
    MyString result;
    delete[] result.String;
    result.String = tmp;
    result.Size = Size+rhs.Size;      
    
    return result;
    

    operator+= is a bit more tricky, because you need to manipulate the current object’s buffer:

    char* tmp = new char[Size + rhs.Size + 1]; // +1 for the terminating '\0'      
    for(int i = 0; i < Size ; i++)      
    {      
      tmp[i] = String[i]; 
    }
    for(int i = 0; i < rhs.Size+1; i++) // +1 to copy the terminating '\0' as well
    {      
      tmp[i+Size] = rhs.String[i]; 
    }
    delete[] String;
    String = tmp;
    Size += rhs.Size;      
    
    return *this;
    

    Update: I assume you also call delete[] in the class destructor — which you should. It is also not hard to imagine, you will want to perform assingment from one MyString object to another. This will lead to cosider the rule of three: If you need any one of desgtructor, copy-constructor or assignment operator, you most likely need all three.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help on both the visual and the programmatic end of this one.
Need help implement this scenario in Hibernate inheritance Cow and Dog are inheriting Animal
I have several questions I need help with. I'll add both my code and
Need help writing a script downloads data from google insight using c# this is
need help in error in database pivot. i have table tamed table_score like below:
I need help finishing this statement. It is frustrating that two of the PHP
I need help about URL rewriting for image gallery for site. This is problem,
Please help to complete this execution of an assignment overloading function. Here is the
I need help with parameteres. Do both of these function definitions do the exact
I need help with the displayed image below. The two buttons edit and back

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.