LinkedList icon indicating copy to clipboard operation
LinkedList copied to clipboard

it is hard to judge get() success or not?

Open m2mer opened this issue 7 years ago • 2 comments

template<typename T> T LinkedList<T>::get(int index){ ListNode<T> *tmp = getNode(index);

return (tmp ? tmp->data : T());

} no matter success or not, an instance of T is returned, so how to judge?

m2mer avatar Apr 28 '18 03:04 m2mer

I'm confused. If there is no data, a new object is malloc'd and returned? or what is going on here...

RyanAveryBSU avatar Oct 15 '18 04:10 RyanAveryBSU

If there is no data, a new object of type T is default-constructed and returned. For simple types like int\char it won't allocate memory, just put zero value into some register and return.

CAHEK7 avatar Jun 13 '19 22:06 CAHEK7