Original Post
I'm helping a friend with something, and he has this template class and I am wondering if it creates a memory leak, or if this would be cleaned up properly by the stack. If it does create a leak, what is a better solution? Fyi, this is for a Fixed point variable, where m_value = the data contained and shift is the number of bytes to the left the whole (integer part) of the variable would start. Is derefercing that pointer in the overloaded operator + a bad idea? Initially he had the first line create a stack variable and then returned it, but he was concerned with speed, because it seemed to create another variable (or so he thought because the copy constructor would be called again during the return statement). Comments, questions concerns are welcome. thnx, tHom.
//copy constructor
inline Fixed(const Fixed<T, shift>& fixed) : m_value(fixed.m_value) { };
//function
template <class T, int shift>
inline Fixed<T, shift>& Fixed<T, shift>::operator+(const Fixed<T, shift>& fixed)
{
Fixed<T, shift> *result = new Fixed<T, shift>(*this);
result->m_value = result->m_value + fixed.m_value;
return *result;
}