I have a class with various members that I only assign at the constructor phase:
class Pet {
const int id;
std::string name;
Pet(int iid, std::string nname) {
id = iid;
if (nname != "") { name = nname; }
else { name = "Johnny Doey"; }
}
Pet() : Pet{ -1, "" } {}
}
I'm fairly new at C++, but wouldn't it make sense to make these members const
? However, simply adding the const
keyword doesn't work ("expression must be a modifiable lvalue"), so how would I do it?