Reputation: 15
Im a first year student and while going through my Linked List note's I saw a copy constructor been used. Previously when I study ADT Array based Implementation, I havent see a copy constructor. Why the copy constructor is been used in the Linked List? Is that important to implement a copy constructor?
Upvotes: 0
Views: 376
Reputation: 385174
Without actually seeing the code it's impossible to say for sure, but I will say that for a linked-list implementation it is highly likely that a copy constructor and assignment operator are required, in order to prevent your class from having broken copy semantics with respect to dynamic allocations performed by your constructor, and de-allocations performed by your destructor.
If you saw an implementation in the past that didn't have these, it is highly likely that it was broken/buggy/wrong, and this is not at all unusual out there on the internet where endless terrible beginner implementations exist and are passed off as "how you should do it" in non-peer-reviewed tutorials. It's even fairly common in University teaching material, sadly.
Learn C++ from a good book, and read about the Rule of Three.
Then delete your implementation and use std::list
!
Upvotes: 1