Does using an assignment operator instead of copy c’tor in generic structures, considered as bad coding? Or it doesn’t matter because perhaps it’s widely assumed that if there is a special c’tor for a certain data type, there should be an appropriate assignment operator too?
For example if I’m making my own generic linked list, using
template
and DATA data;
field inside each node. So the question is if in some place on my list code I can use assignment operator between two DATA variables.
Assignment operator and copy constructor and two distinct things though having similar functionality. As for the question that it is considered a bad practice or not, generally it is not a bad idea to do so. The advantage it has is that it can be called from anywhere at almost any time in the program. Copy constructor is usually only invoked at the initialization of an object.
Assignment operator could lead to very dangerous situations sometimes. The programmer is responsible for checking against certain conditions for example self assignment. Here, in case of your linked list, you would also have to destroy the previous list which the object did hold before new assignment.
I hope this was what you wanted to ask.