What is the difference between std::shared_ptr<T> and std::shared_ptr<T const>?
And when would you use one versus the other?
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.
shared_ptr<int>is ashared_ptrto an non-constint. You can modify the int and theshared_ptrshared_ptr<const int>is ashared_ptrto aconst int. You can’t modify theconst inttheshared_ptrpoints to, because it’sconst. But you can modify theshared_ptritself (assign to it, call other non-const methods, etc)const shared_ptr<int>is aconst shared_ptrto a non-constint. You can’t modify theshared_ptr(by callingresetor any non-const method), but you can modify theintit points toconst shared_ptr<const int>is aconst shared_ptrto aconst int. You can’t modify jack.