Doubt about const member and containers
-
Hi all,
I've got a doubt about the usage of const applied to a single class member like in the following:@
class Article{
private:
const QString id;
QString& description;public:
// methods
}
@What I'd like to express is that the id of an article cannot be changed after the article has been changed. However to use articles with qt containers I have to provide a default constructor and an operator=, and my doubt is about how to implement them and which sense do they have (assigning an article to another does not make sense to me since the id is fixed). Can somebody explain me if this is wrong in design or not?
-
How about you just allow to set the Id using the constructor (or if its automatically generated set it in the constructor). Then you only provide public getter function and no setter function.
-
[quote author="KA51O" date="1336459769"]How about you just allow to set the Id using the constructor (or if its automatically generated set it in the constructor). Then you only provide public getter function and no setter function.[/quote]
yes of course this is a solution, but it will let me to erronously modify the id inside the class methods. So, from a design perspective, it is much more clean the use of const.
-
Sry I didn't consider the case of modifying the data inside the class.