Please conform if I am correct and tell me whether there is a better solution:
I understand that objects with constant members like int const width;
can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities:
- Don't use constant members. (Not a solution)
- Implement my own assignment operator.
- Use some other container that does not need assignment operators
Is that correct? Are there other elegant solutions?
Also I wonder whether I can:
- (4) Force the compiler to create a assignment operator that deals with constant members! (I don't understand why this is such a big problem. Why is the operator not intelligent enough to use initialization lists internally? Or am I missing something?)
- (5) Tell QList that I will never use assignment operations in the list.
EDIT: I never assign objects of this class myself. They are only created by the copy constructor or by an overloaded constructor. So the assignment operator is only required by the container not by myself.
EDIT2: This is the assignment operator I created. I am not sure if its correct though. Cell has a two parameter constructor. These parameters set the two constant members with initialization lists. But the object also contains other variable (non const) members.
Cell& Cell::operator=(Cell const& other)
{
if (this != &other) {
Cell* newCell = new Cell(other.column(), other.row());
return *newCell;
}
return *this;
}
EDIT3: I found this thread with almost the same question: C++: STL troubles with const class members All answers combined together answered my questions.