Thread: returning a temp. object problem

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    returning a temp. object problem

    ok i have a class name CSoulSeeker. i overloaded the addition
    operator with this function header CSoulSeeker operator+(const CSoulSeeker& aSoul); the return statement says this return CSoulSeeker(this->m_HitPoints + aSoul.m_HitPoints);

    from what i read this is returning a local object...doesnt the local object cease to exist (object is destroyed) onces it reaches end of scope...here is the code if you want to look at it..(this is just an example i am working on to understand more about classes....)

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    class CSoulSeeker
    {
    	private:
    		int m_HitPoints;
    
    	public:
    		// Constructors
    		CSoulSeeker();
    		CSoulSeeker(int x);
    		CSoulSeeker(CSoulSeeker& aSoul);
    
    		// Destructor
    		~CSoulSeeker();
    
    		// overloaded assignment operator
    		CSoulSeeker& operator=(const CSoulSeeker& aSoul);
    
    		// overloaded addition operator
    		CSoulSeeker operator+(const CSoulSeeker& aSoul);
    
    		// overloaded greater than operator
    
    };
    
    CSoulSeeker::CSoulSeeker()
    {
    	m_HitPoints = 1000;
    }
    
    CSoulSeeker::CSoulSeeker(int x)
    {
    	m_HitPoints = x;
    }
    
    CSoulSeeker::CSoulSeeker(CSoulSeeker& aSoul)
    {
    	this->m_HitPoints = aSoul.m_HitPoints;
    }
    
    CSoulSeeker::~CSoulSeeker()
    {
    
    }
    CSoulSeeker& CSoulSeeker::operator=(const CSoulSeeker& aSoul)
    {
    	this->m_HitPoints = aSoul.m_HitPoints;
    	return *this;
    }
    
    CSoulSeeker CSoulSeeker::operator+(const CSoulSeeker& aSoul)
    {
    	return CSoulSeeker(this->m_HitPoints + aSoul.m_HitPoints);
    }
    
    
    int main()
    {
    	return 0;
    }
    thanks for the help
    nextus, the samurai warrior

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    This is such an important optimization it has a name....

    The return value optimsation.

    It works by returning constructor arguments. A temporary object is made and returned by value but the standard decrees that where possible temporary objects can be eliminated by the compiler. It is possible for the compiler to eliminate the temporary made in this return statement. So on good compiler this is efficient.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. DirectMusic engine problem
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-17-2005, 06:12 PM
  3. Linked List Part 2
    By Nish in forum C Programming
    Replies: 18
    Last Post: 03-09-2005, 05:05 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM