Temporary object debacle

This is a discussion on Temporary object debacle within the C++ Programming forums, part of the General Programming Boards category; I have an overloaded ~ operator...which returns a temp object...which I can't get around because that's the way my professor ...

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Temporary object debacle

    I have an overloaded ~ operator...which returns a temp object...which I can't get around because that's the way my professor would like. I have an assignment operator for objects as well....basically I have the call..... t21 = ~s21. But when t21 goes to assign s21 to it, there's nothing in the member buf because it got ran through the deconstructor.... (I think)... So I'm at a wall with my limited knowledge. Any thoughts....(besides not returning temp objects)...

    Code:
    ReverseString ReverseString::operator~()
    {
    	int size = m_length;
    	int i;
    	int j;
    
    	char* temp = new char[size];
    
    	for(i = m_length - 1, j = 0; i >= 0; i--, j++)
    	{
    		temp[j] = m_buf[i];
    	}
    
    	temp[size] = '\0';
    	
    	ReverseString rev(temp);
    
    	return rev;
    }
    
    
    
    ReverseString& ReverseString::operator=(const ReverseString& rhs)
    {
       if(this == &rhs)
       {
    	   return *this;
       }
    	m_length = rhs.m_length;
    	if(NULL != m_buf)
    	{
    		delete[]m_buf;
    		m_buf = NULL;
    	}
        m_buf = new char[m_length + 1];
    	strcpy_s(m_buf, m_length + 1, rhs.m_buf);
      	   
    	return *this;
    }
    This will be one of my last posts as I'm graduating in three weeks....and I still don't know anything...LOL!!!!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    As a rough rule, if you have to hand-roll an assignment operator (as you have above) you also need to hand-roll a copy constructor. Among other things, that is necessary to ensure that operations involving copying to and from temporaries can work correctly.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> temp[size] = '\0';
    Also, note that this line accesses memory beyond the bounds of your array. Notice how you add one to the size when allocating in the other function you posted. You should probably do the same in the ReverseString function.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > temp[size] = '\0';
    This steps off the end of your array.

    Also, you don't have a delete [ ] temp;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2008, 01:34 PM
  2. Temporary object destruction
    By Mortissus in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2006, 06:47 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. returning a temporary class object
    By nextus in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21