问题
I am stuck with using immutable objects with collections. Let assume I have the following class :
//in .h
class Data {
public:
Data(const DataObj& data);
Data(const Data&);
const DataObj& getDataObj() const;
private:
const DataObj _data; //actually several objects or simple type
}
inline const DataObj& Data::getDataObj() const {return _data};
//in .c
Data(const DataObj& data) : _data(data){};
Data(const Data& original) : _data(original._data){}
The issue is that when I want to use collections, I have the error
in member function Data&Data::operator(const Data&);
instantiation from 'typename QMap<Key,T>::iterator QMap<Key, T>::insert(const Key&, const T&)[with Key = int, T = Data]'
instantiation from here
error : non-static const member 'const DataObj Data::_data', can't use default assignment operator
Now defining an assignment operator doesn't seems to make sense, since its prototype will be
Data& operator=(const Data&);
What should I do? Am I forced to remove all constant qualifiers in order to use my class inside collections? Or use pointers?
回答1:
Use good stl containers instead of bad Qt:
This will fail to work with Qt container due to COW, but it ok (until you try to copy the container) with stl
class C
{
C operator=(const C&);
};
int main()
{
std::map<int, C> cc;
cc.insert(std::make_pair(0, C()));
}
回答2:
If you are instantiating your map like this
QMap <MyKey, Data>
I guess you should always define an assignment operator for Data (default or your own).
You should try using pointers, as you suggest, like this
QMap <Mykey, Data*>
QMap <Mykey, QSharedPointer<Data>>
If you take a look to the QMap code http://code.woboq.org/kde/include/QtCore/qmap.h.html, some operators/members return < T >, so it would need assignment defined.
回答3:
You can make the data member non-const, but provide only const access to users of the class, except for the assignment operator:
class Data {
public:
Data(const DataObj& data);
Data(const Data&);
Data& operator=(const Data&);
const DataObj& getDataObj() const;
private:
DataObj _data;
};
In this way, assignment is the only operation that can mutate an existing object. You would then have to make sure you provide access to const instances of this type in any public interfaces.
来源:https://stackoverflow.com/questions/12495226/immutable-object-in-collections-c-and-qt