So how i can do this? So that no member function can change the value of its data members once object has been initialized in C++.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Make all the member functions
const. That’s the only mechanism for the job, and it works just fine. If you also make themprivateyou’re completely covered.If for some reason you feel compelled to mark them protected, then things are more complicated.
You will need to make the individual fields
const, and that will in turn require you to initialize them via the member initialization list, or aconst_castof this in the constructor. Or maybe a mutable ctor, but I’m not sure there is such a thing.