Based on this
Stroustrup suggests that “A pointer in a function should not represent ownership”
Question> Can someone give me a little detail explanation? Best if an example is illustrated.
Thank you
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.
A pointer is “owned” by some code if that code is responsible for deleting it, or for transferring ownership to someone else. Various smart pointers implement explicit ownership models.
shared_ptrrepresents multiple pieces of code owning a pointer.unique_ptrrepresents only one piece of code that owns the pointer.What he’s saying is that if a function has a naked pointer (a pointer not in a smart pointer), it should not be considered to own it. If it is to claim some ownership of this pointer, it should either have been given a smart pointer as a parameter, or it should have stored the pointer it created with
newin a smart pointer.He’s saying that only smart pointers own pointers. If a function takes a naked pointer as a parameter, it does not claim ownership of that pointer. If a function returns a naked pointer, you cannot claim ownership of that pointer.