+ 1

const shared pointer copy issue | Custom shared pointer

Refer code below: https://www.sololearn.com/compiler-playground/c6Sga08lo7x4 Is my implementation correct? I am yet to implement move constructor and move assignment operator. But feel free to share your valuable feedback on this implementation. There is a compiler error due to copy constructor. Refer last few lines of main function. ptr1 is a constant object. And as it is constant object, copy constructor expects copy constructor signature must be as below: my_shared_ptr(const my_shared_ptr<T>& pOther); But doing this also results into a compiler issue. pOther cannot be constant because it will not allow below line: ++(*pOther.m_intCnt); In short, can anyone please suggest to avoid compiler error? And also feel free to share other feedback as well.

3rd Sep 2023, 5:07 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Respostas
0
have you try it ( make pOther constant) ? It is true that <pOther..m_intCnt> has type <int * const> when <pOhter> is constant, but it is not an <int const * const>. in other words, you cannot change <pOther.m_intCnt>, but you can change <*pOther.m_intCnt>. ( the pointer is constant, but not the value pointed to.) so "++(*pOther.m_intCnt);" is perfectly valid when <pOhter> is constant.
3rd Sep 2023, 6:30 PM
MO ELomari
MO ELomari - avatar
0
Thanks.!
4th Sep 2023, 3:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar