[SOLVED]-Pointers- When to use them
-
Hi All,
Sorry for asking such trivial questions but need clear them for my exam.
@
Film* findFilm(QString id) const;
@In the above statement the function is returning a pointer to the Film object. Am I correct?
Why should we return pointers instead of objects? Is it to save memory?
Thanks again.
-
Hi,
Yes, that code will return a pointer.
One of the situations where it's better to use pointers is when you need to pass objects between functions. If you pass a object instance as a parameter, the object memory area will be copied for each function that you use it. If you pass a pointer, just a pointer to the object memory area will be copied for the functions. In this way you will save memory and time.
Another situation to use pointers is when you need to create dynamically objects. If you use variables of object, it can be destroyed at the end of the function whereas created.
This subject can generate hours of conversation. This is just a overview of using pointers.
[]'s