Unnecessary and/or temporary objects are frequent culprits that can throw all your hard work — and your program’s performance — right out the window. How can you spot them and avoid them?



Problem



JG Question



1. What is a temporary object?

Guru Question



2. You are doing a code review. A programmer has written the following function, which uses unnecessary temporary objects in at least three places. How many can you identify, and how should the programmer fix them?

string find_addr( list<employee> emps, string name ) {

for( auto i = begin(emps); i != end(emps); i++ ) {

if( *i == name ) {

return i->addr;

}

}

return "";

}

Do not change the operational semantics of this function, even though they could be improved.