T SortedList<T>::Delete(void) {
T temp(m_pFront->GetData());
// do the deletion logic
return temp;
}
Force return-by-reference function to make a copy?
If you really wanted to do this you could use:
If you really wanted to do this you could use:
T SortedList<T>::Delete(void) {
T temp(m_pFront->GetData());
// do the deletion logic
return temp;
}
Wait...so is the C-style initialization the only way to copy the reference value? In other words, this does work / is not allowed?
T noCopy = m_pFront->GetData();
That should work if T has a copy constructor. It will make a copy though, so noCopy is not a good name for the variable ;)
T temp(initializer); and T temp = initializer; will both create a copy of the the object. However, the first form is a direct initializer and the second form is a copy initializer. Under most circumstances you can treat them as the same, but there are times when the first will work but the second won't.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement