-1

I am trying to overloading a string class.In the code shown below I am expecting only overloaded Assignment operator to be invoked but overloaded copy constructor is also invoked. Can anybody please suggest if there is a mistake in my code or my understanding is different?

#include<iostream>

using namespace std;

class String_Overload{
 char *s_ptr;
 public:
    String_Overload()
    { 
        s_ptr = new char(1);
        s_ptr[0]='\0';
        cout<<"In no param constructor"<<"\n";
    }
    
    String_Overload(const char* s_ptr_cpy)
    {
      int len=0;
      while(s_ptr_cpy[len]!=NULL)
      {++len;}
      s_ptr = new char(len+1);
      for(int i=0;i<len;i++)
      {
          s_ptr[i]=s_ptr_cpy[i];
      } 
      s_ptr[len+1]='\0';
      cout<<"In overloaded constructor"<<"\n";
    }

    String_Overload(const String_Overload& s_ptr_cpy)
    {
      int len=0;
      while(s_ptr_cpy.s_ptr[len]!=NULL)
      {++len;}
      s_ptr = new char(len+1);
      for(int i=0;i<len;i++)
      {
          s_ptr[i]=s_ptr_cpy.s_ptr[i];
      } 
      s_ptr[len+1]='\0';   
      cout<<"In copy constructor"<<"\n";
    }
    
    String_Overload operator=(String_Overload& s_ptr_cpy)
    {
      int len=0;
      while(s_ptr_cpy.s_ptr[len]!=NULL){++len;}
      for(int i=0;i<len;i++)
      {
          s_ptr[i]=s_ptr_cpy.s_ptr[i];
      } 
      s_ptr[len+1]='\0';
      cout<<"In assignment constructor\n";
      return *this;

    }
    
    void display(){cout<<s_ptr<<"\n";}
    
    ~String_Overload(){delete []s_ptr;}
};
int main()
{
    char temp[]="Hello World";
    String_Overload s1{temp};
    String_Overload s4;
    s4=s1;
    s4.display();
    return 0;
}

Output:

In overloaded constructor
In no param constructor
In assignment constructor
In copy constructor
Hello World
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    Use a debugger? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason May 23 '23 at 16:02
  • 2
    The expression `new char(len+1)` allocates ***one single*** `char` element, and initialize its value to `len + 1`. You want `new char[en+1]`. – Some programmer dude May 23 '23 at 16:03
  • 3
    In assignment `operator=` you're returning by value. Change the signature of the assignment operator to `String_Overload& operator=(String_Overload&)` – Jason May 23 '23 at 16:03
  • Once you get the copy constructor working, the assignment operator could be this simple: `String_Overload& operator=(String_Overload s_ptr_cpy) { std::swap(s_ptr_cpy.s_ptr, s_ptr); return *this; }`. Note that currently, you have duplicate code in both the copy constructor and assignment operator, so the swap trick (known as the copy/swap idiom) eliminates the duplication and is guaranteed to work. – PaulMcKenzie May 23 '23 at 16:23
  • Your assignmentment should `delete` old pointer, otherwise there's no point. Your use of `NULL` is also improper. It should be compared to pointers, not elements; and it is obsolete; you must use `nullptr`. The way you are implementing it, you can simply use `std::strdup` and `std::free` instead of `new` and `delete`. Array form of dynamic allocation is `new char[N]` with rectangular brackets. – Red.Wave May 23 '23 at 17:10

1 Answers1

2

I am expecting only overloaded Assignment operator to be invoked but overloaded copy constructor is also invoked

This is because the assignment operator= in your examples returns by value.

You can solve this by changing the return type to String_Overload& so that it returns by reference as shown below:

//-------------v--------------------------------------->added & so that we return by reference
String_Overload& operator=(String_Overload& s_ptr_cpy)
{
        //....
  return *this;

}

I would also recommend reading what is return type of assignment operator?

Jason
  • 36,170
  • 5
  • 26
  • 60