Click here to Skip to main content
15,889,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
even though i m passing refrenced argument t = operator what is destructer destroying......
the output is as follows
hello
world
-------
= operator is called
des is called //what is this destructer called for
hello
des is called
des is called

What I have tried:

#include <iostream>
#include<string.h>

using namespace std;
class strin{
    int len;
    char *ptr;
    public:

    strin(){len=0;ptr=NULL;}
    strin(   const char  *s)
    {
          len=strlen(s);
         ptr=new char[len+1];
         strcpy(ptr,s);
    }
    strin( const strin &A)
    {
        len=A.len;
        ptr=new char[len+1];
        strcpy(ptr,A.ptr);
        cout<<"cpy cnst is called\n";

    }
    void print(void)
    {
        cout<<ptr<<"\n";
    }


    strin operator + ( const strin  &obj )
    {
     strin temp;
     temp.len=len+obj.len;
      temp.ptr=new char[temp.len+1];
     strcpy(temp.ptr,ptr);
     strcat(temp.ptr,obj.ptr);
     cout<<"+ is called \n";
     return temp;
    }
~strin()
{
    delete[]ptr;
    cout<<"des is called\n";
}
strin operator = (const strin &x)
{
    len=x.len;
    ptr=new char[len+1];
    strcpy(ptr,x.ptr);
    cout<<"= operator is called\n";

}
};

int main()
{


  strin x("hello");
  strin y("world");
  x.print();
  y.print();

  cout<<"--------\n";

  y=x;
  y.print();

  }
Posted
Updated 24-May-18 9:05am
Comments
[no name] 24-May-18 11:11am    
I think you Need first to fix your Operator= the signature and also return value.

const strin& operator=(const strin &rhs)
John R. Shaw 24-May-18 13:17pm    
The return type should not be const.
T& T::operator=(const T&)
[no name] 24-May-18 15:28pm    
Thank you for this hint. I never thought about it. But does it have a disadvantage to return const?
Thanks in advance.
John R. Shaw 25-May-18 14:28pm    
Returning a 'const' breaks normal behavior.
a = b = c; // 'const' or no 'const' this works
(a = b) = c; // is legal, but will not work if assignment operator returns 'const'
Which is why the default assignment operator is specified by the standard to return 'T&' instead of 'const T&'.
[no name] 25-May-18 14:38pm    
Thank you very much for this! Regards.

That is because most probably your return value (by value) goes into nirvana and will be destructed ... questionable ...

This is the correct implementation of Operator= which will also not call the destructor:
C++
const strin& operator = (const strin &x)
{
    cout<<"= operator is called\n";
    if (this != &x)
    {
       // TODO: In case ptr allready has Memory allocated, one Need to release it 
       //       first. 
       // 

       // Assign
       len=x.len;
       ptr=new char[len+1];
       strcpy(ptr, x.ptr);
    }
    return(*this);
}

Attention, possible memory leak
The above is still not quite correct. You Need also check first wheter this has allready allocated Memory and release it first in case. That will be your exercise


A side note, my personal preferenced way how I combine operator= and copy constructor

a.) Copy constructor
C++
MyClass::MyClass (const MyClass &rhs)
	: InitMember_1(),
          .....
	  InitMember_N()
{
	operator= (rhs);
}

b.) operator=
C++
const MyClass& MyClass::operator=(const MyClass &rhs)
{
   if (this != &rhs)
   {
     // TODO: Implement 
   }
   return(*this);
}
 
Share this answer
 
v7
Comments
CPallini 24-May-18 12:15pm    
Check for identity is a good point. My 5.
I think a correct implementation should also delete previously allocated memory (if present).
Pardon the pedantic attitude.
[no name] 24-May-18 15:08pm    
I added this Memory leak Thing at the same time you wrote your comment :)
Thank you for your 5.
Member 12914219 24-May-18 12:45pm    
what is the purpose of returning (*this);.
[no name] 24-May-18 15:26pm    
That allows some like (which personally I don't like):
a= b= c;

Assuming you define your Operator= like this void operator = (const strin &x) you can not chain assignements.
CPallini 24-May-18 15:32pm    
It is in the signature of the operator
https://en.cppreference.com/w/cpp/language/copy_assignment
The rationale is shown, for instance, in this page
http://icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
 
Share this answer
 
Comments
Maciej Los 24-May-18 16:02pm    
5ed!
CPallini 25-May-18 2:03am    
Thank you!
The following examples give the strong guarantee, which is what you should strive for.
You should use assert(this != &rhs) to catch self assignment mistakes during development. There are only two reasons for checking self assignment: (1) avoid unneeded allocations, (2) the object is intended to hold large amounts of data.

// Add swap to your class
// swap is one of your best friends
// because it is guaranteed not to throw.
void swap(strin& x) noexcept
{
	using std::swap;
	swap(len, x.len);
	swpa(ptr, x.ptr);
}

// Version 1
// This version behaves the same in all cases,
// even on the rare occasion you do self assignment
strin& operator=(const strin& rhs)
{
	cout << "= operator is called" << endl;
	// Using a constructor here provides the strong
	// guarantee that this object will not be modified
	// if an exception is thrown.
	strin temp(rhs);
	swap (*this, temp);
    return(*this);
}

// Version 2
// The difference between this and version 1
// is that temporary object is built on the stack
// when the operator is called.
strin& operator=(strin rhs)
{
	cout << "= operator is called" << endl;
	swap (*this, rhs);
    return(*this);
}

// Version 3
// Basically the same as version 1
strin& operator=(const strin& rhs)
{
	cout << "= operator is called" << endl;
	// first use temporaries in case an exception is thrown.
	char temp_ptr = new char [rhs.len+1];
	strcpy(temp_ptr, rhs.ptr);
	
	// No exception were thrown, so we can modify this.
	delete [] ptr;
	ptr = temp_ptr;
	len = rhs.len;
	
    return(*this);
}
 
Share this answer
 
Comments
Maciej Los 24-May-18 16:03pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900