I’ve a class String, which presents a dynamical array of characters, which cannot be changed after initialization. So I need to initialize it, and then prevent further changes to the argument.
How can I do this? 🙂
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.
You make your member
const. And you do the initialization in the constructor.EDIT: As @ildjarn pointed out, take extra care when using immutable objects. You have to be certain that’s what you actually need. You won’t be able to use them in standard containers, or have logical copying.